解组转义JSON字符串

解组转义JSON字符串,json,go,Json,Go,我试图从一个API中解组一个JSON对象,该API在JSON内部有一个字符串,该字符串本身就是JSON,但它作为字符串转义。它看起来像这样: { "duration": "126.61ms", "startTime": "2016-02-19T20:01:17.884Z", "total": 123, "content": [ { "dateCreated": "2016-02-19T20:01:09.181Z", "lastUpdated": "2016-0

我试图从一个API中解组一个JSON对象,该API在JSON内部有一个字符串,该字符串本身就是JSON,但它作为字符串转义。它看起来像这样:

{
  "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}"
  }
}
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"`
}
我正试图将其解包到如下结构中:

{
  "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}"
  }
}
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"`
}

将转义的JSON对象放入stats结构而不是字符串中的最佳方法是什么?

分两个阶段进行。首先使用stats字段将外部对象解组为字符串,然后将该字符串解组为stats结构。您可以对
UnmarshalJSON
进行自定义实现,因此这是一种模糊的方法,但无论哪种方法,我所知道的唯一合理的方法是分别执行这两种方法

我正在使用。它确实易于使用、理解和迭代。过来看。