Golang 使用 DYNAMIC 键解析 json [英] Golang parse a json with DYNAMIC key
本文介绍了Golang 使用 DYNAMIC 键解析 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 json 字符串如下:
I have a json string as follows:
j := `{"bvu62fu6dq": {
"name": "john",
"age": 23,
"xyz": "weu33s"
.....
.....}
}`
我想从上面的 json 字符串中提取 name
和 age
的值.我查看了 golang 站点 http://play.golang.org/p/YQgzP7KPp9
I want to extract the value of name
and age
from above json string. I looked at this example given at golang site http://play.golang.org/p/YQgzP7KPp9
但我的问题是顶级json中的关键是动态的.这意味着 bvu62fu6dq
是动态的.我已经创建了这样的结构:
But my problem is the key in the json on top level is dynamic. That means bvu62fu6dq
is dynamic. I have created struct like this:
type Info struct {
UniqueID map[string]string
}
但不确定如何提取name
和age
.我的代码位于 http://play.golang.org/p/Vbdkd3XIKc
But not sure how to extract name
and age
. My code is at http://play.golang.org/p/Vbdkd3XIKc
推荐答案
我相信你想要这样的:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
type Info map[string]Person
然后,在解码之后:
fmt.Printf("%s: %d
", info["bvu62fu6dq"].Name, info["bvu62fu6dq"].Age)
完整示例:http://play.golang.org/p/FyH-cDp3Na一个>
这篇关于Golang 使用 DYNAMIC 键解析 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文