ioutil.ReadAll and unmarshal on nested curl response returns error due to problem in key of array struct

huangapple go评论67阅读模式
英文:

ioutil.ReadAll and unmarshal on nested curl response returns error due to problem in key of array struct

问题

给你提供一些上下文,我正在使用curl命令访问一个第三方的端点,响应的格式类似于以下内容:

{
    "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": "Success",
    "data": {
        "list": {
            "1": {
                "user": "user A",
                "status": "normal"
            },
            "2": {
                "user": "user A",
                "status": "normal"
            }
        },
        "page": 1,
        "total_pages": 2000
    }
}

请问你能教我如何正确读取响应或者如何解析这个响应吗?谢谢!

英文:

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

答案1

得分: 1

你可以像这样定义你的结构体:

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

这段代码定义了一个名为User的结构体,它包含了Code、Message和Data三个字段。其中,Data字段又包含了List、Page和TotalPages三个字段。List字段是一个映射类型,键为字符串,值为一个包含User和Status两个字段的结构体。这些字段都使用了json标签来指定在JSON序列化和反序列化时的名称。

英文:

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

huangapple
  • 本文由 发表于 2022年9月29日 21:17:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/73896231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定