為了給你上下文,我正在卷曲到第三方端點,響應類似于這個{ "code": 200, "message": "Success", "data": { "list": [ { "user": "user A", "status" : "normal" }, { "user": "user B", "status" : "normal" } ], "page": 1, "total_pages": 5000 }}我的結構類似于type User struct { Code int `json:"code"` Message string `json:"message"` Data struct { List []struct { User string `json:"user"` Status string `json:"status"` } `json:"list"` Page int `json:"page"` TotalPages int `json:"total_pages"` } `json:"data"`}請檢查我的代碼defer response.Body.Close()io_response, err := ioutil.ReadAll(response.Body)returnData := User{}err = jsoniter.Unmarshal([]byte(io_response), &returnData)if err != nil { log.Println(err)}上面的代碼返回錯誤decode slice: expect [ or n, but found {, error found in #10 byte of ...|:{"list":{"1"當我執行 fmt.Println(string(io_response)) 時,返回結果如下:{ "code": 200, "message": "成功", "data": { "list": { "1": { "user": "user A", "status": "normal" }, "2 ": { "user": "user A", "status": "normal" } }, "page": 1, "total_pages": 2000 } }你能教我如何正確閱讀回復或如何解組嗎?謝謝
1 回答

絕地無雙
TA貢獻1946條經驗 獲得超4個贊
你可以這樣定義你的結構:
type User struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
List map[string]struct {
User string `json:"user"`
Status string `json:"status"`
} `json:"list"`
Page int `json:"page"`
TotalPages int `json:"total_pages"`
} `json:"data"`
}
- 1 回答
- 0 關注
- 192 瀏覽
添加回答
舉報
0/150
提交
取消