从字符串中删除反斜杠

我要将字符串发送到服务器。我的字符串如下所示,</ p>
\ n

  str:=“ [{\” name \“:\” cpu \“,\” status \“:\%d \”},{\“ name \”:\“ LTE \  “,\”状态\“:\”%d \“},{\”名称\“:\”网络\“,\”状态\“:\”%d \“},{\”名称\“:  \“内存\”,\“状态\”:\“%d \”}]“ 
</ code> </ pre>

当我使用” fmt.Println(str)打印“时 ”,它将给出以下期望的输出,</ p>

  [{“ name”:“ cpu”,“ status”:“%d”},{“ name”:“ LTE  “,”状态“:”%d“},{”名称“:”网络“,”状态“:”%d“},{”名称“:”内存“,”状态“:”%d“}]

</ code> </ pre>

但是当我向服务器发送相同的字符串时,服务器接收到的字符串如下,</ p>

 “ [{  \“名称\”:\“ cpu \”,\“状态\”:\“%d \”},{\“名称\”:\“ LTE \”,\“状态\”:\“%d \  “},{\”名称\“:\”网络\“,\”状态\“:\”%d \“},{\”名称\“:\”内存\“,\”状态\“:\  “%d \”}]“ 
</ code> </ pre>

请找到我的代码段,如下所示:</ p>

  func(m  * MetricSet)Fetch()(common.MapStr,错误){
var x字符串
x = fmt.Sprin tf(“ [{{\” name \“:\” cpu \“,\” status \“:\”%d \“},{\” name \“:\” LTE \“,\” status \“: \“%d \”},{\“名称\”:\“网络\”,\“状态\”:\“%d \”},{\“名称\”:\“内存\”,\“ status \“:\”%d \“}]”“,17,26,34,33)
fmt.Println(x)
事件:= common.MapStr {
” cpu_status“:(m.cpu_status% 4),
“ memory_status”:(m.memory_status%4),
“ lte_status”:(m.lte_status%4),
“ network_status”:(m.network_status%4),
“ summary” :x,
}

m.cpu_status ++
m.memory_status = m.memory_status + 2
m.lte_status = m.lte_status + 7
m.network_status = m.network_status + 13

返回事件,nil
}
</ code> </ pre>

如何解决?请帮助我。</ p>
</ div>

展开原文

原文

I want to send the string to the server.My string is like as below,

  str := "[{\"name\":\"cpu\",\"status\":\"%d\"}, {\"name\":\"LTE\",\"status\":\"%d\"}, {\"name\":\"Network\",\"status\":\"%d\"}, {\"name\":\"Memory\",\"status\":\"%d\"}]"

When I print it using "fmt.Println(str)",it gives desired output as below,

[{"name":"cpu","status":"%d"}, {"name":"LTE","status":"%d"}, {"name":"Network","status":"%d"}, {"name":"Memory","status":"%d"}]

But when I am sending same string to server,server receives string as below,

"[{\"name\":\"cpu\",\"status\":\"%d\"}, {\"name\":\"LTE\",\"status\":\"%d\"}, {\"name\":\"Network\",\"status\":\"%d\"}, {\"name\":\"Memory\",\"status\":\"%d\"}]"

Please find my code snippet as below:

func (m *MetricSet) Fetch() (common.MapStr, error) {
        var x string
        x =fmt.Sprintf("[{\"name\":\"cpu\",\"status\":\"%d\"}, {\"name\":\"LTE\",\"status\":\"%d\"}, {\"name\":\"Network\",\"status\":\"%d\"}, {\"name\":\"Memory\",\"status\":\"%d\"}]", 17,26,34,33)
        fmt.Println(x)
        event := common.MapStr{
                "cpu_status": (m.cpu_status%4),
                "memory_status" : (m.memory_status%4),
                "lte_status" : (m.lte_status%4),
                "network_status" : (m.network_status%4),
                "summary": x,
        }

        m.cpu_status++
        m.memory_status = m.memory_status + 2
        m.lte_status = m.lte_status + 7
        m.network_status = m.network_status + 13

        return event, nil
}

How to solve it?Please help me.

dongwei1895
dongwei1895 是的。我正在以json格式发送数据。但是服务器端也只能像以前一样
大约 3 年之前 回复
doupinge9055
doupinge9055 什么是common.MapStr?您是否使用JSON格式发送事件?如果是,则在将事件编组为JSON时,将转义双引号,即“将转换为\”。在您的情况下,应如何在服务器端接收数据(事件的预期JSON)?
大约 3 年之前 回复
dousi1875
dousi1875 有问题的代码段
大约 3 年之前 回复
doumao9363
doumao9363 您是怎么发送的?请提供代码段。
大约 3 年之前 回复

2个回答



您将摘要内容作为字符串发送,因此,您需要将其作为地图片段发送</ p> \ n

  k:= [... common.MapStr {
{“ name”:“ cpu”,“ status”:m.cpu_status},
{“ name”:“ LTE “,”状态“:m.lte_status},
{”名称“:”网络“,”状态“:m.network_status},
{”名称“:”内存“,”状态“:m.memory_status} ,
}
event:= common.MapStr {
“ cpu_status”:(m.cpu_status%4),
“ memory_status”:(m.memory_status%4),
“ lte_status”:(m.lte_status %4),
“ network_status”:(m.network_status%4),
“ summary”:k,
}
</ code> </ pre>

请参见 https://play.golang.org/p/yTSXnNKclG </ p>
</ div>

展开原文

原文

You are sending the content of summary as a string so, instead you need to send it as a slice of maps

k := [...]common.MapStr{
    {"name": "cpu", "status": m.cpu_status},
    {"name": "LTE", "status": m.lte_status},
    {"name": "Network", "status": m.network_status},
    {"name": "Memory", "status": m.memory_status},
}
event := common.MapStr{
    "cpu_status":     (m.cpu_status % 4),
    "memory_status":  (m.memory_status % 4),
    "lte_status":     (m.lte_status % 4),
    "network_status": (m.network_status % 4),
    "summary":        k,
}

See https://play.golang.org/p/yTSXnNKclG

When event is marshalled into JSON, the value of summary will be escaped if it contains a double quote. The quick and dirty solution will be defined the event as map[string]interface{} to be able to store arbitrary type as map value, and store the summary value as json.RawMessage e.g.

event := map[string]interface{}{
    "cpu_status": (m.cpu_status%4),
    "memory_status" : (m.memory_status%4),
    "lte_status" : (m.lte_status%4),
    "network_status" : (m.network_status%4),
    "summary": json.RawMessage(x),
}

However, you need to make sure that the value of x is a valid JSON object (which is error prone). More robust solution is by defining the items in summary as a struct type, e.g.

type Status struct {
    Name string `json:"name"`
    Status string `json:"status"`
}
summary := []*Status{
    &Status{"cpu", "17"},
    &Status{"LTE", "26"},
    &Status{"Network", "34"},
    &Status{"Memory", "33"},
}

event := map[string]interface{}{
    "cpu_status": (m.cpu_status%4),
    "memory_status" : (m.memory_status%4),
    "lte_status" : (m.lte_status%4),
    "network_status" : (m.network_status%4),
    "summary": summary,
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐