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

将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"}
  ]
}
  • 写回答

1条回答 默认 最新

  • duan1989643 2018-06-06 07:37
    关注

    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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同