dscdttg4389
2017-08-01 06:10
浏览 811
已采纳

从字符串中删除反斜杠

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.

图片转代码服务由CSDN问答提供 功能建议

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

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

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

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

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

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

请找到我的代码段,如下所示:

  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 
} 
   
 
 

如何解决?请帮助我。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题