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

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

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

问题

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

  1. {
  2. "code": 200,
  3. "message": "Success",
  4. "data": {
  5. "list": [
  6. {
  7. "user": "user A",
  8. "status": "normal"
  9. },
  10. {
  11. "user": "user B",
  12. "status": "normal"
  13. }
  14. ],
  15. "page": 1,
  16. "total_pages": 5000
  17. }
  18. }

你的结构体定义如下:

  1. type User struct {
  2. Code int `json:"code"`
  3. Message string `json:"message"`
  4. Data struct {
  5. List []struct {
  6. User string `json:"user"`
  7. Status string `json:"status"`
  8. } `json:"list"`
  9. Page int `json:"page"`
  10. TotalPages int `json:"total_pages"`
  11. } `json:"data"`
  12. }

你的代码如下:

  1. defer response.Body.Close()
  2. io_response, err := ioutil.ReadAll(response.Body)
  3. returnData := User{}
  4. err = jsoniter.Unmarshal([]byte(io_response), &returnData)
  5. if err != nil {
  6. log.Println(err)
  7. }

以上代码返回了一个错误:

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

当你使用fmt.Println(string(io_response))打印响应内容时,结果如下:

  1. {
  2. "code": 200,
  3. "message": "Success",
  4. "data": {
  5. "list": {
  6. "1": {
  7. "user": "user A",
  8. "status": "normal"
  9. },
  10. "2": {
  11. "user": "user A",
  12. "status": "normal"
  13. }
  14. },
  15. "page": 1,
  16. "total_pages": 2000
  17. }
  18. }

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

英文:

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

  1. {
  2. "code": 200,
  3. "message": "Success",
  4. "data": {
  5. "list": [
  6. {
  7. "user": "user A",
  8. "status" : "normal"
  9. },
  10. {
  11. "user": "user B",
  12. "status" : "normal"
  13. }
  14. ],
  15. "page": 1,
  16. "total_pages": 5000
  17. }
  18. }

My struct is similar to

  1. type User struct {
  2. Code int `json:"code"`
  3. Message string `json:"message"`
  4. Data struct {
  5. List []struct {
  6. User string `json:"user"`
  7. Status string `json:"status"`
  8. } `json:"list"`
  9. Page int `json:"page"`
  10. TotalPages int `json:"total_pages"`
  11. } `json:"data"`
  12. }

Please check my codes

  1. defer response.Body.Close()
  2. io_response, err := ioutil.ReadAll(response.Body)
  3. returnData := User{}
  4. err = jsoniter.Unmarshal([]byte(io_response), &returnData)
  5. if err != nil {
  6. log.Println(err)
  7. }

The code above returns an error

  1. 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

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

  1. type User struct {
  2. Code int `json:"code"`
  3. Message string `json:"message"`
  4. Data struct {
  5. List map[string]struct {
  6. User string `json:"user"`
  7. Status string `json:"status"`
  8. } `json:"list"`
  9. Page int `json:"page"`
  10. TotalPages int `json:"total_pages"`
  11. } `json:"data"`
  12. }

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

英文:

you can define your struct like this:

  1. type User struct {
  2. Code int `json:"code"`
  3. Message string `json:"message"`
  4. Data struct {
  5. List map[string]struct {
  6. User string `json:"user"`
  7. Status string `json:"status"`
  8. } `json:"list"`
  9. Page int `json:"page"`
  10. TotalPages int `json:"total_pages"`
  11. } `json:"data"`
  12. }

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:

确定