将JSON成员字符串转换为JSON对象
问题描述:
I have this struct:
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"`
}
and this is my json result:
{
"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\"}]"
}
How can I escaped double quotes in my result data as JSON like this:
{
"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"}
]
}
我有以下结构: p>
type ResponseStatus结构{\ n StatusCode int
消息字符串
数据字符串`json:“ data”`
}
type Pets结构{
ID int`json:“ id”`
名称字符串`json:“ name”
Age int`json:“ age”`
输入字符串`json:“ type”`
}
code> pre>
,这是我的json结果:
{
“ StatusCode”:200,
“ Message”:“ Hello framework-OK”,
“ data”:“ [{\” id \“: 1,\ “名称\”:\ “乔治·\”,\ “年龄\”:2,\ “类型\”:\ “狗\”},{\ “ID \”:2,\ “名称\”: \ “沃尔特\”,\ “年龄\”:1,\ “类型\”:\ “兔\”},{\ “ID \”:3,\ “名称\”:\ “汤姆\”,\” 年龄\ “:1,\” 类型\ “:\” 猫\ “},{\” ID \ “:4,\” 名称\ “:\” 声张\ “\ ”年龄\“:5,\” 类型\ “:\” 狗\ “},{\” ID \ “:5,\” 名称\ “:\” 托特\ “\ ”年龄\“:3,\ ”类型\“:\” 龟\ “},{\” ID \ “:6,\” 名称\ “:\” 杰里\ “\ ”年龄\“:1,\ ”类型\“:\ ”仓鼠\“},{\” ID \ “:7,\” 名称\ “:\” garf \ “\ ”年龄\“:2,\ ”类型\“:\ ”猫\“},{\ ”ID \“:8,\” 名\ “:\” 米洛\”,\ “年龄\”:4,\ “类型\”:\ “狗\”},{\ “ID \”:9,\ “名称\”:\ “基米\”, \ “年龄\”:2,\“TY PE \ “:\” 猫\ “},{\” ID \ “:10,\” 名称\ “:\” 巴克\ “\ ”年龄\“:1,\ ”类型\“:\” 兔\ “}]”
}
code> pre>
如何像这样将JSON中的结果数据转义为双引号: p>
{
“ StatusCode”:200,
“ Message”:“ Hello framework-OK”,
“ data”:[
{“ id”:1,“ name”:“ george”,“ age“:2,” type“:” dog“},
{” id“:2,” name“:”沃尔特“,” age“:1,” type“:”兔子“},
{” 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”:“仓鼠”},
{“ 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”:“兔子” }
]
}
code> pre>
div>
答
You are doing fine, just a few remarks: Remove the quote before and after the square brackets, and you should make Data of type []Pets (which struct I would call Pet, because every item contains a single Pet). The square-brackets are part of the JSON construct. And then you don't need to escape the quotes, because they become JSON identifiers.
In your way, it becomes a single long string, which, clearly, is not what you intent to have.
These are the structures that fit on your second 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"`
}