解析嵌套的JSON结构

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

Unmarshal nested JSON structure

问题

我正在尝试解码以下字符串,但只得到空值。

在Go中,如何解码嵌套的JSON结构?

我想将以下内容转换为映射数据结构。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func main() {
  7. jStr := `
  8. {
  9. "AAA": {
  10. "assdfdff": ["asdf"],
  11. "fdsfa": ["1231", "123"]
  12. }
  13. }
  14. `
  15. type Container struct {
  16. Key string `json:"AAA"`
  17. }
  18. var cont Container
  19. json.Unmarshal([]byte(jStr), &cont)
  20. fmt.Println(cont)
  21. }

请注意,这是一个Go语言代码示例,用于解码嵌套的JSON结构并将其转换为映射数据结构。

英文:

http://play.golang.org/p/f6ilWnWTjm

I am trying to decode the following string but only getting null values.

How do I decode nested JSON structure in Go?

I want to convert the following to map data structure.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func main() {
  7. jStr := `
  8. {
  9. "AAA": {
  10. "assdfdff": ["asdf"],
  11. "fdsfa": ["1231", "123"]
  12. }
  13. }
  14. `
  15. type Container struct {
  16. Key string `json:"AAA"`
  17. }
  18. var cont Container
  19. json.Unmarshal([]byte(jStr), &cont)
  20. fmt.Println(cont)
  21. }

答案1

得分: 24

使用Go中的嵌套结构来匹配JSON中的嵌套结构。

以下是处理示例JSON的一个示例:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. func main() {
  8. jStr := `
  9. {
  10. "AAA": {
  11. "assdfdff": ["asdf"],
  12. "fdsfa": ["1231", "123"]
  13. }
  14. }
  15. `
  16. type Inner struct {
  17. Key2 []string `json:"assdfdff"`
  18. Key3 []string `json:"fdsfa"`
  19. }
  20. type Container struct {
  21. Key Inner `json:"AAA"`
  22. }
  23. var cont Container
  24. if err := json.Unmarshal([]byte(jStr), &cont); err != nil {
  25. log.Fatal(err)
  26. }
  27. fmt.Printf("%+v\n", cont)
  28. }

playground链接

你也可以在内部结构中使用匿名类型:

  1. type Container struct {
  2. Key struct {
  3. Key2 []string `json:"assdfdff"`
  4. Key3 []string `json:"fdsfa"`
  5. } `json:"AAA"`
  6. }

playground链接

或者同时使用外部和内部结构:

  1. var cont struct {
  2. Key struct {
  3. Key2 []string `json:"assdfdff"`
  4. Key3 []string `json:"fdsfa"`
  5. } `json:"AAA"`
  6. }

playground链接

如果你不知道内部结构中的字段名,则可以使用map:

  1. type Container struct {
  2. Key map[string][]string `json:"AAA"`
  3. }

playground链接

还有更多选项。希望这能帮助你找到正确的方法。

英文:

Use nested structs in Go to match the nested structure in JSON.

Here's one example of how to handle your example JSON:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. func main() {
  8. jStr := `
  9. {
  10. "AAA": {
  11. "assdfdff": ["asdf"],
  12. "fdsfa": ["1231", "123"]
  13. }
  14. }
  15. `
  16. type Inner struct {
  17. Key2 []string `json:"assdfdff"`
  18. Key3 []string `json:"fdsfa"`
  19. }
  20. type Container struct {
  21. Key Inner `json:"AAA"`
  22. }
  23. var cont Container
  24. if err := json.Unmarshal([]byte(jStr), &cont); err != nil {
  25. log.Fatal(err)
  26. }
  27. fmt.Printf("%+v\n", cont)
  28. }

playground link

You can also use an anonymous type for the inner struct:

  1. type Container struct {
  2. Key struct {
  3. Key2 []string `json:"assdfdff"`
  4. Key3 []string `json:"fdsfa"`
  5. } `json:"AAA"`
  6. }

playground link

or both the outer and inner structs:

  1. var cont struct {
  2. Key struct {
  3. Key2 []string `json:"assdfdff"`
  4. Key3 []string `json:"fdsfa"`
  5. } `json:"AAA"`
  6. }

playground link

If you don't know the field names in the inner structure, then use a map:

  1. type Container struct {
  2. Key map[string][]string `json:"AAA"`
  3. }

http://play.golang.org/p/gwugHlCPLK

There are more options. Hopefully this gets you on the right track.

答案2

得分: 6

首先:除非你有非常充分的理由,否则不要忽略函数或方法返回的错误。

如果你对代码进行以下更改:

  1. err := json.Unmarshal([]byte(jStr), &cont)
  2. if err != nil {
  3. fmt.Println(err)
  4. }

你将会看到错误信息,告诉你为什么值是空的:

json: 无法将对象解组为类型为字符串的Go值

简单来说:Key 不能是 string 类型,所以你必须使用不同的类型。根据 Key 值的特性,你有几种解码选项:

  • 如果结构是静态的,使用另一个结构来匹配这个结构。
  • 如果结构是动态的,使用 interface{}(或者如果它始终是 JSON 对象类型,则使用 map[string]interface{})。
  • 如果值不需要被访问,只需要在稍后进行 JSON 编码时使用,或者如果解码需要延迟进行,使用 json.RawMessage
英文:

First of all: Don't ignore errors returned by a function or method unless you have a very good reason to do so.

If you make the following change to the code:

  1. err := json.Unmarshal([]byte(jStr), &cont)
  2. if err != nil {
  3. fmt.Println(err)
  4. }

You will see the error message telling you why the value is empty:

>json: cannot unmarshal object into Go value of type string

Simply put: Key cannot be of type string, so you have to use a different type. You have several option on how to decode it depending on the characteristics of the Key value:

  • If the structure is static, use another struct to match this structure.
  • If the structure is dynamic, use interface{} (or map[string]interface{} if it is always of type JSON object)
  • If the value is not to be accessed, only to be used in later JSON encoding, or if the decoding is to be delayed, use json.RawMessage

huangapple
  • 本文由 发表于 2014年9月22日 12:09:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/25966567.html
匿名

发表评论

匿名网友

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

确定