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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧