将JSON解析为嵌套结构体。

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

Parse JSON into nested structs

问题

以下是翻译好的内容:

  1. type APIResponse struct {
  2. Results []Result `json:"results,omitempty"`
  3. Paging Paging
  4. }
  5. type Result struct {
  6. Id string `json:"id"`
  7. Name string `json:"name"`
  8. }
  9. type Paging struct {
  10. Count int `json:"count"`
  11. Previous string `json:"previous"`
  12. Next string `json:"next"`
  13. }
  14. func Get(ctx context.Context) APIResponse[T] {
  15. results := APIResponse{}
  16. rc, Err := r.doRequest(ctx, req)
  17. if rc != nil {
  18. defer rc.Close()
  19. }
  20. err = json.NewDecoder(rc).Decode(&results)
  21. return results
  22. }

示例JSON如下:

  1. {
  2. "count": 70,
  3. "next": "https://api?page=2",
  4. "previous": null,
  5. "results": [
  6. {
  7. "id": 588,
  8. "name": "Tesco"
  9. }
  10. ...
  11. ]
  12. }

我希望将其解码为一个形式为APIResponse的结构体,其中分页元素是一个子结构,就像results一样。然而,在示例JSON中,分页部分没有父级json标签。如何将其解码为一个单独的结构体?

目前,如果我将Count、Next和Previous提升到APIResponse中,它们会出现,但当它们是子结构时,它们不会出现。

英文:
  1. type APIResponse struct {
  2. Results []Result `json:"results,omitempty"`
  3. Paging Paging
  4. }
  5. type Result struct {
  6. Id string `json:"id"`,
  7. Name string `json:"name"`,
  8. }
  9. type Paging struct {
  10. Count int `json:"count"`
  11. Previous string `json:"previous"`
  12. Next string `json:"next"`
  13. }
  14. func Get(ctx context.Context) APIResponse[T] {
  15. results := APIResponse{}
  16. rc, Err := r.doRequest(ctx, req)
  17. if rc != nil {
  18. defer rc.Close()
  19. }
  20. err = json.NewDecoder(rc).Decode(&results)
  21. return results
  22. }

The Sample JSON looks like this:

  1. {
  2. "count": 70,
  3. "next": "https://api?page=2",
  4. "previous": null,
  5. "results": [
  6. {
  7. "id": 588,
  8. "name": "Tesco",
  9. }...

and I want it decoded into a struct of the form APIResponse, where the pagination element is a substruct, like the results is. However, in the sample JSON, the pagination aspect does not have a parent json tag. How can it be decoded into it's own separate struct?

Currently, if I lift Count,Next, and Previous into the APIResponse, they appear, but they don't appear when they're a substruct.

答案1

得分: 1

将你的Paging结构直接嵌入到APIResponse中,如下所示:

  1. type APIResponse struct {
  2. Results []Result `json:"results,omitempty"`
  3. Paging
  4. }
  5. type Result struct {
  6. Id string `json:"id"`
  7. Name string `json:"name"`
  8. }
  9. type Paging struct {
  10. Count int `json:"count"`
  11. Previous string `json:"previous"`
  12. Next string `json:"next"`
  13. }

这样它将按照这个结构定义的方式工作。你可以通过两种方式访问它的字段:

  1. 直接访问:
    APIResponse.Count
  2. 间接访问:
    APIResponse.Paging.Count
英文:

Embed your Paging struct directly into APIResponse like:

  1. type APIResponse struct {
  2. Results []Result `json:"results,omitempty"`
  3. Paging
  4. }
  5. type Result struct {
  6. Id string `json:"id"`,
  7. Name string `json:"name"`,
  8. }
  9. type Paging struct {
  10. Count int `json:"count"`
  11. Previous string `json:"previous"`
  12. Next string `json:"next"`
  13. }

This way it will work as it defined in this structure. You can access its fields two ways:

  1. Directly:
    APIResponse.Count
  2. Indirect:
    APIResponse.Paging.Count

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

发表评论

匿名网友

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

确定