duanhuo7441 2016-02-19 20:10
浏览 93

解组转义的JSON字符串[重复]

This question already has an answer here:

I'm trying to Unmarshal a JSON object from an API that has a string inside of the JSON that itself is JSON, but it's escaped as a string. It looks something like this:

{
  "duration": "126.61ms",
  "startTime": "2016-02-19T20:01:17.884Z",
  "total": 123,
  "content": [
  {
    "dateCreated": "2016-02-19T20:01:09.181Z",
    "lastUpdated": "2016-02-19T20:01:09.181Z",
    "name": "name",
    "stats": "{\"id\":545,\"lastUpdated\":\"2015-01-09T19:16:04.535Z\",\"all\":{\"runs\":{\"count\":123}"
  }
}

I'm trying to unmarshal that into a struct like this:

type RunStatus struct {
    Duration string `json:"duration"`
    StartTime time.Time `json:"startTime"`
    Total int `json:"total"`
    Content []struct {
        DateCreated time.Time `json:"dateCreated"`
        LastUpdated time.Time `json:"lastUpdated"`
        name string `json:"name"`
        stats string `json:"stats"`
    } `json:"content"`
}

What's the best way to get the escaped JSON object into a stats struct rather than it being in a string?

</div>
  • 写回答

1条回答 默认 最新

  • douke3442 2016-02-19 20:14
    关注

    Do this in two phases. First unmarshal the outer object with the stats field as a string, then unmarshal that string into the stats struct. You could do a custom implementation of UnmarshalJSON so this is obfuscated but either way the only reasonable approach I know of is to do the two separately.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类