从字符串中删除反斜杠。

huangapple go评论122阅读模式
英文:

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

  1. m.cpu_status++
  2. m.memory_status = m.memory_status + 2
  3. m.lte_status = m.lte_status + 7
  4. m.network_status = m.network_status + 13
  5. return event, nil

}

如何解决这个问题?请帮助我。

英文:

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

  1. 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,

  1. [{"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,

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

Please find my code snippet as below:

  1. func (m *MetricSet) Fetch() (common.MapStr, error) {
  2. var x string
  3. x =fmt.Sprintf("[{\"name\":\"cpu\",\"status\":\"%d\"}, {\"name\":\"LTE\",\"status\":\"%d\"}, {\"name\":\"Network\",\"status\":\"%d\"}, {\"name\":\"Memory\",\"status\":\"%d\"}]", 17,26,34,33)
  4. fmt.Println(x)
  5. event := common.MapStr{
  6. "cpu_status": (m.cpu_status%4),
  7. "memory_status" : (m.memory_status%4),
  8. "lte_status" : (m.lte_status%4),
  9. "network_status" : (m.network_status%4),
  10. "summary": x,
  11. }
  12. m.cpu_status++
  13. m.memory_status = m.memory_status + 2
  14. m.lte_status = m.lte_status + 7
  15. m.network_status = m.network_status + 13
  16. return event, nil
  17. }

How to solve it?Please help me.

答案1

得分: 1

你正在将摘要内容作为字符串发送,而不是作为映射切片发送。

  1. k := [...]common.MapStr{
  2. {"name": "cpu", "status": m.cpu_status},
  3. {"name": "LTE", "status": m.lte_status},
  4. {"name": "Network", "status": m.network_status},
  5. {"name": "Memory", "status": m.memory_status},
  6. }
  7. event := common.MapStr{
  8. "cpu_status": (m.cpu_status % 4),
  9. "memory_status": (m.memory_status % 4),
  10. "lte_status": (m.lte_status % 4),
  11. "network_status": (m.network_status % 4),
  12. "summary": k,
  13. }

参考链接: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

  1. k := [...]common.MapStr{
  2. {"name": "cpu", "status": m.cpu_status},
  3. {"name": "LTE", "status": m.lte_status},
  4. {"name": "Network", "status": m.network_status},
  5. {"name": "Memory", "status": m.memory_status},
  6. }
  7. event := common.MapStr{
  8. "cpu_status": (m.cpu_status % 4),
  9. "memory_status": (m.memory_status % 4),
  10. "lte_status": (m.lte_status % 4),
  11. "network_status": (m.network_status % 4),
  12. "summary": k,
  13. }

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

答案2

得分: 1

event被编组为JSON时,如果summary包含双引号,其值将被转义。一种快速而简单的解决方案是将event定义为map[string]interface{},以便能够将任意类型存储为映射值,并将summary值存储为json.RawMessage,例如:

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

但是,您需要确保x的值是有效的JSON对象(这容易出错)。更健壮的解决方案是将summary中的项定义为结构类型,例如:

  1. type Status struct {
  2. Name string `json:"name"`
  3. Status string `json:"status"`
  4. }
  5. summary := []*Status{
  6. &Status{"cpu", "17"},
  7. &Status{"LTE", "26"},
  8. &Status{"Network", "34"},
  9. &Status{"Memory", "33"},
  10. }
  11. event := map[string]interface{}{
  12. "cpu_status": (m.cpu_status%4),
  13. "memory_status": (m.memory_status%4),
  14. "lte_status": (m.lte_status%4),
  15. "network_status": (m.network_status%4),
  16. "summary": summary,
  17. }
英文:

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.

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

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.

  1. type Status struct {
  2. Name string `json:"name"`
  3. Status string `json:"status"`
  4. }
  5. summary := []*Status{
  6. &Status{"cpu", "17"},
  7. &Status{"LTE", "26"},
  8. &Status{"Network", "34"},
  9. &Status{"Memory", "33"},
  10. }
  11. event := map[string]interface{}{
  12. "cpu_status": (m.cpu_status%4),
  13. "memory_status" : (m.memory_status%4),
  14. "lte_status" : (m.lte_status%4),
  15. "network_status" : (m.network_status%4),
  16. "summary": summary,
  17. }

huangapple
  • 本文由 发表于 2017年8月1日 14:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/45430126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定