英文:
Remove backslash from the string
问题
我想将字符串发送到服务器。我的字符串如下所示,
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
}
如何解决这个问题?请帮助我。
英文:
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.
答案1
得分: 1
你正在将摘要内容作为字符串发送,而不是作为映射切片发送。
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
英文:
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,
}
答案2
得分: 1
当event
被编组为JSON时,如果summary
包含双引号,其值将被转义。一种快速而简单的解决方案是将event
定义为map[string]interface{}
,以便能够将任意类型存储为映射值,并将summary
值存储为json.RawMessage
,例如:
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),
}
但是,您需要确保x
的值是有效的JSON对象(这容易出错)。更健壮的解决方案是将summary
中的项定义为结构类型,例如:
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,
}
英文:
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,
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论