亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ioutil.ReadAll 和 unmarshal 對嵌套的 curl 響應返回錯誤

ioutil.ReadAll 和 unmarshal 對嵌套的 curl 響應返回錯誤

Go
慕村225694 2023-02-21 13:04:20
為了給你上下文,我正在卷曲到第三方端點,響應類似于這個{    "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"`

}


查看完整回答
反對 回復 2023-02-21
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號