2 回答

TA貢獻1795條經驗 獲得超7個贊
您應該只使用具有實際類型的簡單字段聲明,而不是[]如下所示的類型:
type InLicense struct {
Total int16 json:"total,omitempty"
}
type OutLicense struct {
Pagination InLicense json:"pagination,omitempty"
}
type MetaLicense struct {
Meta OutLicense json:"meta,omitempty"
}

TA貢獻1785條經驗 獲得超4個贊
我稍微簡化了解析,只使用了該json.Unmarshal()函數。
raw := "{\n \"meta\": {\n \"query_time\": 0.039130201,\n \"pagination\": {\n \"offset\": 1345,\n \"limit\": 5000,\n \"total\": 1345\n }\n }\n}"
parsed := &MetaLicense{}
err := json.Unmarshal([]byte(raw), parsed)
if err != nil {
log.Fatal(err)
}
fmt.Println(parsed.Meta.Pagination.Total) // Prints: 1345
這是我使用的類型
type InLicense struct {
Total int16 `json:"total,omitempty"`
}
type OutLicense struct {
Pagination InLicense `json:"pagination,omitempty"`
}
type MetaLicense struct {
Meta OutLicense `json:"meta,omitempty"`
}
如所寫,您提供的 JSON 有一個額外的,內容,這使您的 json 無法解析(假設您也添加了缺少}的 's.
您的 JSON 中沒有列表。列表用[]符號表示。為了讓您的類型正常工作,您的 JSON 必須如下所示:
{
"meta": [{
"query_time": 0.039130201,
"pagination": [{
"offset": 1345,
"limit": 5000,
"total": 1345
}]
}]
}
- 2 回答
- 0 關注
- 278 瀏覽
添加回答
舉報