在Golang中访问类型为map [string] interface {}的嵌套映射 [英] Accessing Nested Map of Type map[string]interface{} in Golang

查看:987
本文介绍了在Golang中访问类型为map [string] interface {}的嵌套映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图解析一个JSON响应。它可以是多层次的。这就是我所做的:

$ $ p $ $ code var结果图字符串接口
json.Unmarshal(apiResponse,& amp; amp; ;结果)

首先,这是正确的方法吗?



假设回应如下:

  {
args:{
foo:bar
}
}

要访问 foo 键,我看到一个 playground 这样做:

  result [args]。(map [string] interface {} )[foo] 

这里,。()符号?这是正确的吗?

解决方案

符号 x。(T)是称为类型断言


对于接口类型和类型 T 的表达式 x ,主表达式 x。(T)断言 x 不是 nil ,并且存储在 x 中的值是类型 T


您的示例:

  result [args]。(map [string] interface {} )[foo] 

这意味着的结果 map关键字args的类型是 map [string] interface {} (另一个带有 string 键和任何值的映射)。并且你想访问与关键字foo关联的那张地图的元素。



如果您知道注意事项关于输入的JSON格式,那么是的,你必须使用一个通用的 map [string]接口{} 类型来处理它。如果您知道输入JSON的确切结构,您可以创建一个 struct 来匹配预期的字段,这样您可以将JSON文本解组为一个您自定义的值 struct 类型,例如:

 类型Point结构{
名称字符串
X,Y int
}

func main(){
in:=`{Name:center,X: 2,Y:3}`

pt:= Point {}
json.Unmarshal([] byte(in),& pt)

fmt.Printf(Result:%+ v,pt)
}

输出:

 结果:{姓名:中心X:2 Y:3} 

Go Playground

为输入建模



您可以使用此类型对当前的JSON输入进行建模:

 类型数据结构{
Args结构{
Foo字符串
}
}

访问 Foo (在

  d:= Data {} 
json.Unmarshal([ ]字节(in),& d)
fmt.Println(Foo:,d.Args.Foo)


So I'm trying to parse a JSON response. It can be multiple levels deep. This is what I did:

var result map[string]interface{}
json.Unmarshal(apiResponse, &result)

Firstly, is this the right way to do it?

Lets say the response was as follows:

{
  "args": {
            "foo": "bar"
          }
}

To access key foo, I saw a playground doing this:

result["args"].(map[string]interface{})["foo"]

Here, what is the .() notation? Is this correct?

解决方案

The notation x.(T) is called a Type Assertion.

For an expression x of interface type and a type T, the primary expression x.(T) asserts that x is not nil and that the value stored in x is of type T.

Your example:

result["args"].(map[string]interface{})["foo"]

It means that the value of your results map associated with key "args" is of type map[string]interface{} (another map with string keys and any values). And you want to access the element of that map associated with the key "foo".

If you know noting about the input JSON format, then yes, you have to use a generic map[string]interface{} type to process it. If you know the exact structure of the input JSON, you can create a struct to match the expected fields, and doing so you can unmarshal a JSON text into a value of your custom struct type, for example:

type Point struct {
    Name string
    X, Y int
}

func main() {
    in := `{"Name":"center","X":2,"Y":3}`

    pt := Point{}
    json.Unmarshal([]byte(in), &pt)

    fmt.Printf("Result: %+v", pt)
}

Output:

Result: {Name:center X:2 Y:3}

Try it on the Go Playground.

Modeling your input

Your current JSON input could be modelled with this type:

type Data struct {
    Args struct {
        Foo string
    }
}

And accessing Foo (try it on the Go Playground):

d := Data{}
json.Unmarshal([]byte(in), &d)
fmt.Println("Foo:", d.Args.Foo)

这篇关于在Golang中访问类型为map [string] interface {}的嵌套映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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