Go结构用于解析JSON数组

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

Go structure for unmarshalling a JSON array

问题

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

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

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

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

  1. {
  2. "petfinder": {
  3. "lastOffset": {
  4. "$t": 5
  5. },
  6. "pets": {
  7. "pet": [
  8. {
  9. "options": {
  10. "option": [
  11. {
  12. "$t": "altered"
  13. },
  14. {
  15. "$t": "hasShots"
  16. },
  17. {
  18. "$t": "housebroken"
  19. }
  20. ]
  21. },
  22. "breeds": {
  23. "breed": {
  24. "$t": "Dachshund"
  25. }
  26. }
  27. },
  28. {
  29. "options": {
  30. "option": {
  31. "$t": "hasShots"
  32. }
  33. },
  34. "breeds": {
  35. "breed": {
  36. "$t": "American Staffordshire Terrier"
  37. }
  38. },
  39. "shelterPetId": {
  40. "$t": "13-0164"
  41. },
  42. "status": {
  43. "$t": "A"
  44. },
  45. "name": {
  46. "$t": "HAUS"
  47. }
  48. }
  49. ]
  50. }
  51. }
  52. }

提前致谢。

英文:

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:

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

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

  1. {
  2. "petfinder": {
  3. "lastOffset": {
  4. "$t": 5
  5. },
  6. "pets": {
  7. "pet": [
  8. {
  9. "options": {
  10. "option": [
  11. {
  12. "$t": "altered"
  13. },
  14. {
  15. "$t": "hasShots"
  16. },
  17. {
  18. "$t": "housebroken"
  19. }
  20. ]
  21. },
  22. "breeds": {
  23. "breed": {
  24. "$t": "Dachshund"
  25. }
  26. }
  27. },
  28. {
  29. "options": {
  30. "option": {
  31. "$t": "hasShots"
  32. }
  33. },
  34. "breeds": {
  35. "breed": {
  36. "$t": "American Staffordshire Terrier"
  37. }
  38. },
  39. "shelterPetId": {
  40. "$t": "13-0164"
  41. },
  42. "status": {
  43. "$t": "A"
  44. },
  45. "name": {
  46. "$t": "HAUS"
  47. }
  48. }
  49. ]
  50. }
  51. }
  52. }

Thanks in advance.

答案1

得分: 2

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

  1. type Option struct {
  2. Key string
  3. Value string
  4. }
  5. type Data struct {
  6. Name string
  7. Options []Option
  8. }
英文:

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:

  1. {
  2. "name": "something",
  3. "options": [
  4. {
  5. "key": "a",
  6. "value": "b"
  7. },
  8. {
  9. "key": "c",
  10. "value": "d"
  11. },
  12. {
  13. "key": "e",
  14. "value": "f"
  15. },
  16. ]
  17. }

You need this Data type in Go:

  1. type Option struct {
  2. Key string
  3. Value string
  4. }
  5. type Data struct {
  6. Name string
  7. Options []Option
  8. }

答案2

得分: 1

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

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

例如:

  1. type Option struct {
  2. Property string `json:"$t,omitempty"`
  3. }

可能出现的$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:

  1. type Option struct {
  2. Property string `json:"$t,omitempty"`
  3. }

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:

确定