為了給你上下文,我正在卷曲到第三方端點(diǎn),響應(yīng)類似于這個(gè){ "code": 200, "message": "Success", "data": { "list": [ { "user": "user A", "status" : "normal" }, { "user": "user B", "status" : "normal" } ], "page": 1, "total_pages": 5000 }}我的結(jié)構(gòu)類似于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"`}請(qǐng)檢查我的代碼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)}上面的代碼返回錯(cuò)誤decode slice: expect [ or n, but found {, error found in #10 byte of ...|:{"list":{"1"當(dāng)我執(zhí)行 fmt.Println(string(io_response)) 時(shí),返回結(jié)果如下:{ "code": 200, "message": "成功", "data": { "list": { "1": { "user": "user A", "status": "normal" }, "2 ": { "user": "user A", "status": "normal" } }, "page": 1, "total_pages": 2000 } }你能教我如何正確閱讀回復(fù)或如何解組嗎?謝謝
1 回答

絕地?zé)o雙
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
你可以這樣定義你的結(jié)構(gòu):
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 關(guān)注
- 189 瀏覽
添加回答
舉報(bào)
0/150
提交
取消