我有这个结构:
type ResponseStatus struct {
StatusCode int
Message string
Data string `json:"data"`
}
type Pets struct {
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Type string `json:"type"`
}
这是我的json结果:
{
"StatusCode": 200,
"Message": "Hello framework - OK",
"data": "[{\"id\":1,\"name\":\"george\",\"age\":2,\"type\":\"dog\"},{\"id\":2,\"name\":\"walter\",\"age\":1,\"type\":\"rabbit\"},{\"id\":3,\"name\":\"tom\",\"age\":1,\"type\":\"cat\"},{\"id\":4,\"name\":\"doggo\",\"age\":5,\"type\":\"dog\"},{\"id\":5,\"name\":\"torto\",\"age\":3,\"type\":\"turtle\"},{\"id\":6,\"name\":\"jerry\",\"age\":1,\"type\":\"hamster\"},{\"id\":7,\"name\":\"garf\",\"age\":2,\"type\":\"cat\"},{\"id\":8,\"name\":\"milo\",\"age\":4,\"type\":\"dog\"},{\"id\":9,\"name\":\"kimi\",\"age\":2,\"type\":\"cat\"},{\"id\":10,\"name\":\"buck\",\"age\":1,\"type\":\"rabbit\"}]"
}
如何将结果数据中的双引号转义为json,如下所示:
{
"StatusCode": 200,
"Message": "Hello framework - OK",
"data": [
{"id": 1,"name": "george","age": 2,"type": "dog"},
{"id": 2,"name": "walter","age": 1,"type": "rabbit"},
{"id": 3,"name": "tom","age": 1,"type": "cat"},
{"id": 4,"name": "doggo","age": 5,"type": "dog"},
{"id": 5,"name": "torto","age": 3,"type": "turtle"},
{"id": 6,"name": "jerry","age": 1,"type": "hamster"},
{"id": 7,"name": "garf","age": 2,"type": "cat"},
{"id": 8,"name": "milo","age": 4,"type": "dog"},
{"id": 9,"name": "kimi","age": 2,"type": "cat"},
{"id": 10,"name": "buck","age": 1,"type": "rabbit"}
]
}
提前谢谢。
答案 0 :(得分:2)
你做得很好,只是一些评论:删除方括号前后的引号,你应该制作类型[] Pets的数据(我称之为Pet的结构,因为每个项目都包含一个Pet)。方括号是JSON构造的一部分。然后你不需要转义引号,因为它们成为JSON标识符。
以你的方式,它变成一个长长的字符串,显然,这不是你想要的。
这些结构适合您的第二个JSON
type ResponseStatus struct {
StatusCode int
Message string
Data []Pet `json:"data"`
}
type Pet struct {
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Type string `json:"type"`
}