這是 API 響應數據,看起來像這樣。{ "result":1, "message":"", "pds": [ { "state":"Y", "code":13, "name":"AAA", "price":39900, }, { "state":"Y", "code":12, "name":"BBB", "price":38000, } ], "request": { "op":"new", }}我如何在 Go 語言中獲取這些數據?我試過json.Unmarshall了,map[string]interface{}但看起來我使用了錯誤的類型來獲取數據。我應該使用結構嗎?
2 回答

回首憶惘然
TA貢獻1847條經驗 獲得超11個贊
如果您不希望json.Unmarshall輸出是一個map[string]interface{}.
如果將此 JSON 對象映射到 Go 結構,您會發現以下結構:
type APIResponse struct {
Result int `json:"result"`
Message string `json:"message"`
Pds []struct {
State string `json:"state"`
Code int `json:"code"`
Name string `json:"name"`
Price float64 `json:"price"`
} `json:"pds"`
Request struct {
Op string `json:"op"`
} `json:"request"`
}
您還可以在此處找到將 JSON 對象轉換為 Go 結構的好工具
- 2 回答
- 0 關注
- 103 瀏覽
添加回答
舉報
0/150
提交
取消