【发布时间】:2018-07-12 11:25:40
【问题描述】:
我有一些如下所示的 JSON:
{
"ABC": {"symbol": "abc", "open": 42},
"DEF": {"symbol": "abc", "open": 42},
"GHI": {"symbol": "abc", "open": 42}
}
我不需要 ABC/DEF/GHI 部分,只需要右边的部分。在我的代码中,ABC、DEF 和 GHI 的值是 entity.Day 类型,如下所示:
type Day struct {
Symbol string `json:"symbol" sql:"symbol"`
Date time.Time `json:"date" sql:"date"`
OpenP float64 `json:"open" sql:"open"`
HighP float64 `json:"high" sql:"high"`
LowP float64 `json:"low" sql:"low"`
CloseP float64 `json:"close" sql:"close"`
VolumeP float64 `json:"volume" sql:"volume"`
Label string `json:"label" sql:"-"`
ChangeOverTime float64 `json:"changeOverTime" sql:"change_over_time"`
UnadjustedVolume float64 `json:"unadjustedVolume" sql:"unadjusted_volume"`
Change float64 `json:"change" sql:"change"`
ChangePercent float64 `json:"changePercent" sql:"change_percent"`
VWAP float64 `json:"vwap" sql:"vwap"`
}
还有其他端点产生entity.Days,但这是唯一一个具有这种结构的端点。理想情况下,如何将 JSON 解组为 entity.Days 数组?
我的第一个想法是做一个中间数据结构:
type previous struct {
tckrs map[string]entity.Day
}
p := previous{tckrs: make(map[string]entity.Day)}
json.Unmarshal(res, &p)
该代码生成一个空结构,json.Unmarshal 返回一个 nil 错误。你能帮帮我吗?
PS - 我搜索了很多,发现了类似的答案,还有很多其他人尝试map 方法,尽管这对我不起作用。
【问题讨论】:
-
json.Unmarshal(res,&p.tckrs)?
标签: json go unmarshalling