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

从字符串中删除反斜杠

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.

  • 写回答

2条回答 默认 最新

  • duangutian1426 2017-08-01 07:16
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥20 小规模孤立词识别系统设计
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?