gpt4 book ai didi

string - 从字符串中删除反斜杠

转载 作者:IT王子 更新时间:2023-10-29 02:02:55 28 4
gpt4 key购买 nike

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

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

当我使用“fmt.Println(str)”打印它时,它会给出如下所需的输出,

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

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

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

请找到我的代码片段如下:

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
}

如何解决?请帮助我。

最佳答案

您将摘要的内容作为字符串发送,因此您需要将其作为 map slice 发送

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,
}

参见 https://play.golang.org/p/yTSXnNKclG

关于string - 从字符串中删除反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430126/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com