从字符串中删除反斜杠。

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

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

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

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

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:

确定