Go结构用于解析JSON数组

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

Go structure for unmarshalling a JSON array

问题

所以我有一些JSON(由PetFinder API提供),其中有一个JSON数组“pet”。我想使用“encoding/json”包从中解组,得到一个宠物结构的切片。这种结构会是什么样子?我找不到任何关于unmarshal函数如何处理JSON数组的示例。

这是我计划在有了正确的结构后要做的事情:

pfetch := new(PetsFetcher) //其中PetsFetcher是我要求的结构
err := json.Unmarshal(body, &pfetch)

这是body中的json(以ascii字节切片的形式):

{
  "petfinder": {
    "lastOffset": {
      "$t": 5
    },
    "pets": {
      "pet": [
        {
          "options": {
            "option": [
              {
                "$t": "altered"
              },
              {
                "$t": "hasShots"
              },
              {
                "$t": "housebroken"
              }
            ]
          },
          "breeds": {
            "breed": {
              "$t": "Dachshund"
            }
          }
  	},
        {
          "options": {
            "option": {
              "$t": "hasShots"
            }
          },
          "breeds": {
            "breed": {
              "$t": "American Staffordshire Terrier"
            }
          },
          "shelterPetId": {
            "$t": "13-0164"
          },
          "status": {
            "$t": "A"
          },
          "name": {
            "$t": "HAUS"
          }
        }
      ]
    }
  }
}

提前致谢。

英文:

So I have some JSON (courtesy of the PetFinder API) that has a JSON array "pet". I want to unmarshal from it, using the "encoding/json" package, a slice of pet structs. What would this kind of structure look like? I can't find any examples of how the unmarshall function handles JSON arrays.

Here's what I was planning to do once I had a proper struct:

pfetch := new(PetsFetcher) // where PetsFetcher is the struct im asking for
err := json.Unmarshal(body, &pfetch)

And here's the json that is in body (in the form of a slice of ascii bytes):

{
  "petfinder": {
    "lastOffset": {
      "$t": 5
    },
    "pets": {
      "pet": [
        {
          "options": {
            "option": [
              {
                "$t": "altered"
              },
              {
                "$t": "hasShots"
              },
              {
                "$t": "housebroken"
              }
            ]
          },
          "breeds": {
            "breed": {
              "$t": "Dachshund"
            }
          }
  	},
        {
          "options": {
            "option": {
              "$t": "hasShots"
            }
          },
          "breeds": {
            "breed": {
              "$t": "American Staffordshire Terrier"
            }
          },
          "shelterPetId": {
            "$t": "13-0164"
          },
          "status": {
            "$t": "A"
          },
          "name": {
            "$t": "HAUS"
          }
        }
      ]
    }
  }
}

Thanks in advance.

答案1

得分: 2

我真的不知道你的JSON中的那些$t属性是做什么的,所以让我们用一个简单的例子来回答你的问题。要解析这个JSON,你需要在Go中定义这个Data类型:

type Option struct {
    Key   string
    Value string
}

type Data struct {
    Name    string
    Options []Option
}
英文:

I really have no idea what those $t attributes are doing there in your JSON, so let’s answer your question with a simple example. To unmarshal this JSON:

{
  "name": "something",
  "options": [
    {
      "key": "a",
      "value": "b"
    },
    {
      "key": "c",
      "value": "d"
    },
    {
      "key": "e",
      "value": "f"
    },
  ]
}

You need this Data type in Go:

type Option struct {
    Key   string
    Value string
}

type Data struct {
    Name    string
    Options []Option
}

答案2

得分: 1

你可以将一个javascript数组解组成一个切片。编组/解组规则在json包Marshal下面有描述。

要解组看起来像“$t”的键,你需要注释它将要解包的结构体。

例如:

type Option struct {
    Property string `json:"$t,omitempty"`
}

可能出现的$t是一个错误,应该是字典中的键。

英文:

You can unmarshal a javascript array into a slice. The marhsal/unmarshalling rules are described under Marshal in the json package.

To unmarshal keys that look like "$t", you'll have to annotate the struct that it'll unpack into.

For example:

type Option struct {
    Property string `json:"$t,omitempty"`
}

It may be that the $t that appear are a mistake, and are supposed to be keys in a dictionary.

huangapple
  • 本文由 发表于 2013年6月16日 10:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/17129767.html
匿名

发表评论

匿名网友

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

确定