去Unmarshal嵌套的JSON结构 [英] Go Unmarshal nested JSON structure

查看:83
本文介绍了去Unmarshal嵌套的JSON结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去Unmarshal嵌套JSON结构



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



我试图解码下面的字符串,但只能得到空值。



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



我想将以下内容转换为地图数据结构。




$ b pre $

$ b $(

$ encoding / json
fmt


func main(){
jStr:=`
{
AAA :{
assdfdff:[asdf],
fdsfa:[1231,123]
}
}
`
类型容器结构{
键字符串`json:AAA`
}
var cont容器

json.Unmarshal([] byte(jStr), & cont)
fmt.Println(cont)
}


解决方案

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



下面是如何处理您的示例JSON的一个示例:

  package main 

$ import
encoding / json
fmt
log


func main(){
jStr :=
{
AAA:{
assdfdff:[asdf],
fdsfa:[1231,123]



$ b类型内部结构{
Key2 [] string`json:assdfdff`
Key3 [] string`json :fdsfa`
}
类型容器struct {
Key内部``json:AAA`
}
var cont容器
如果err: = json.Unmarshal([] byte(jStr),& cont); err!= nil {
log.Fatal(err)
}
fmt.Printf(%+ v \ n,cont)
}

游乐场链接



您也可以使用匿名类型作为内部结构:

 类型Container struct {
键结构{
Key2 [] string`json:assdfdff`
Key3 [] string`json:fdsfa`
}`json: AAA`
}

游乐场链接



或同时包含外部和内部结构:

<$
Key2 []字符串`json:assdfdff`
Key3 []字符串`json:fdsfa `
}`json:AAA`
}

游乐场链接



如果您不知道内部的字段名称结构,然后使用地图:

 类型Container结构{
键映射[string] [] string`json:AAA`
}

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



还有更多选项。希望这会让你走上正轨。


Go Unmarshal nested JSON structure

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.

Please let me know.

package main

import (
  "encoding/json"
  "fmt"
)

func main() {
  jStr := `
{
    "AAA": {
        "assdfdff": ["asdf"],
        "fdsfa": ["1231", "123"]
    }
}
`
  type Container struct {
    Key string `json:"AAA"`
  }
  var cont Container

  json.Unmarshal([]byte(jStr), &cont)
  fmt.Println(cont)
}

解决方案

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

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

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

func main() {
    jStr := `
    {
        "AAA": {
            "assdfdff": ["asdf"],
            "fdsfa": ["1231", "123"]
        }
    }
    `

    type Inner struct {
        Key2 []string `json:"assdfdff"`
        Key3 []string `json:"fdsfa"`
    }
    type Container struct {
        Key Inner `json:"AAA"`
    }
    var cont Container
    if err := json.Unmarshal([]byte(jStr), &cont); err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%+v\n", cont)
}

playground link

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

type Container struct {
    Key struct {
        Key2 []string `json:"assdfdff"`
        Key3 []string `json:"fdsfa"`
    }  `json:"AAA"`
}

playground link

or both the outer and inner structs:

var cont struct {
    Key struct {
        Key2 []string `json:"assdfdff"`
        Key3 []string `json:"fdsfa"`
    } `json:"AAA"`
}

playground link

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

type Container struct {
    Key map[string][]string `json:"AAA"`
}

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

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

这篇关于去Unmarshal嵌套的JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆