2016-02-19 75 views
0

我想從一個JSON對象中取出一個JSON對象,該對象中有一個JSON字符串,它本身是JSON,但它作爲字符串轉義。它看起來是這樣的:Unmarshal Escaped 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"` 
} 

什麼來獲得逃脫JSON對象到統計數據的最佳方式結構,而不是它在一個字符串?

+0

我正在使用https://github.com/antonholmquist/jason。這是非常容易使用和理解和迭代。一探究竟。 – pregmatch

回答

0

分兩步進行。首先用stats字段將外部對象解組爲字符串,然後將該字符串解組到stats結構中。你可以做一個UnmarshalJSON的自定義實現,所以這是混淆,但無論哪種方式,我知道的唯一合理的方法是分別做兩個。