dongzhizhai4070
2018-06-06 07:22
采纳率: 0%
浏览 90
已采纳

将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"}
  ]
}

图片转代码服务由CSDN问答提供 功能建议

我有以下结构:</ 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结果:< / p>

  {
“ 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>

1条回答 默认 最新

相关推荐 更多相似问题