1

To give you context, I am curling to a third party endpoint, the response is similar to this one

{
    "code": 200,
    "message": "Success",
    "data": {
        "list": [
            {
               "user": "user A",
               "status" : "normal"
            },
            {
                "user": "user B",
               "status" : "normal"
            }
        ],
        "page": 1,
        "total_pages": 5000
    }
}

My struct is similar to

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"`
}

Please check my codes

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)
}

The code above returns an error

decode slice: expect [ or n, but found {, error found in #10 byte of ...|:{"list":{"1"

When I do fmt.Println(string(io_response)), it was returned like this:

{ "code": 200, "message": "Success", "data": { "list": { "1": { "user": "user A", "status": "normal" }, "2": { "user": "user A", "status": "normal" } }, "page": 1, "total_pages": 2000 } }

Can you please teach me how to read the response properly or how to unmarshal this? Thank you

yssachan
  • 109
  • 7
  • 1
    The error says you are trying to decode into a slice, but the json starts with `{`. The `"list"` key in the json is another object, not an array. – JimB Sep 29 '22 at 13:19
  • OH..... YOU ARE RIGHT!!!!! hahahahhahahhahaha thank you so much – yssachan Sep 29 '22 at 13:24
  • 1
    FYI [ioutil.ReadAll](https://pkg.go.dev/io/ioutil#ReadAll) is deprecated (as is the entire `io/ioutil` pkg) since go `1.16` - use [io.ReadAll](https://pkg.go.dev/io#ReadAll) instead – colm.anseo Sep 29 '22 at 13:43
  • Whenever you need to modify your code to post it, make sure to test it first to make sure it still reproduces the problem. If it doesn't, it might even lead you to the solution. For example, the code you posted works fine: https://go.dev/play/p/x6rNQwhs7OK – Adrian Sep 29 '22 at 13:57
  • Apologies @Adrian, here's my original question go playground https://go.dev/play/p/WxK5PD2rg9T jiahua, gave an answer I should declare it as map[string]struct instead of []struct – yssachan Sep 30 '22 at 00:26
  • @colm.anseo thank you so much will change to io.ReadAll – yssachan Sep 30 '22 at 00:27

1 Answers1

1

you can define your struct like this:

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"`
}
jiahua
  • 36
  • 2