所以我試圖分析JSON到一些結構和工程確定以下各項:地圖結構從JSON在Golang
type train struct {
ID string `json:"id"`
Price float64 `json:"price,string"`
Distance float64 `json:"Distance,string"`
}
type Station struct {
ID int64 `json:"id,string"`
arrTrain []train`json:"arr"`
depTrain []train`json:"dep"`
}
但問題是,我想能夠輕鬆要使用它們的ID引用arrTrain和depTrain中的項目,所以我認爲我需要更改Station結構以arrTrain和depTrain作爲以ID爲關鍵字的映射。這是可能的時候解析的JSON或它必須'後處理'?
編輯: 作爲一個評論說,不幸的是我的JSON是以下形式:
{
"id":1,
"arr": [
{"id":"one","price":"$10.1","Distance":"100km"},
{...}
],
"dep":[
{"id":"one","price":"$10.1","Distance":"100km"},
{...}
]
}
換句話說,ID是不是在JSON對象之外,arrTrain是列表。
我嘗試了下面的一些答案,似乎在結構內形成的地圖不被json.Marshal識別/丟失 - [playground](https://play.golang.org/p/5-yQvqi14Q ) – Conner
使這些字段可導出https://play.golang.org/p/x8TOcjcj7T – Treesrule14