将 json 字符串解组为具有结构本身的一个元素的结构 [英] Unmarshal json string to a struct that have one element of the struct itself
问题描述
我是初学者并试图解组以下 json 字符串
I am go beginner and trying to unmarshal the following json string
[{
"db": {
"url": "mongodb://localhost",
"port": "27000",
"uname": "",
"pass": "",
"authdb": "",
"replicas": [
{
"rs01": {
"url":"mongodb://localhost",
"port": "27001",
"uname": "",
"pass": "",
"authdb": ""
}
},
{
"rs02": {
"url":"mongodb://localhost",
"port": "27002",
"uname": "",
"pass": "",
"authdb": ""
}
}
]
}
}]
这是结构
type DBS struct {
URL string `json:url`
Port string `json:port`
Uname string `json:uname`
Pass string `json:pass`
Authdb string `json:authdb`
Replicas []DBS `json:replicas`
}
这是函数
func loadConfigs() []DBS {
var config []DBS
raw, err := ioutil.ReadFile("./config.json")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
json.Unmarshal(raw, &config)
return config
}
函数正在返回
{ []}
推荐答案
您的 JSON 输入不是 DBS
的一部分,因为还有另一个 JSON 对象包装器,以及 DBS 的值
属于 "db"
属性.
Your JSON input is not a slice of DBS
, as there is another JSON object wrapper, and a value of DBS
belongs to the property "db"
.
更深入地说,replicaps"
是一个 JSON 数组,其中的对象持有不同的键,它们的值可以由 DBS
表示.
Going deeper, the "replicaps"
is a JSON array with objects holding varying keys, their values being representable by DBS
.
因此,要完整描述您的 JSON,您需要某种动态"类型.例如,地图就是这样一种动态类型.
So to fully describe your JSON, you need some kind of "dynamic" type. A map is such a dynamic type for example.
因此您的原始 JSON 输入可以完全建模为以下类型:[]map[string]DBS
.这是一片地图,因为您的 JSON 输入包含一个 JSON 数组.并且映射键可以建模任何属性名称,值是由 DBS
结构建模的 JSON 对象.
So your original JSON input can be fully modeled with the type: []map[string]DBS
. It's a slice of maps, as your JSON input contains a JSON array. And the map key can model any property name, and the value is a JSON object modeled by the DBS
struct.
查看这个完全解析 JSON 输入的示例:
See this example which fully parses the JSON input:
type DBS struct {
URL string `json:"url"`
Port string `json:"port"`
Uname string `json:"uname"`
Pass string `json:"pass"`
Authdb string `json:"authdb"`
Replicas []map[string]DBS `json:"replicas"`
}
func main() {
var dbs []map[string]DBS
if err := json.Unmarshal([]byte(src), &dbs); err != nil {
panic(err)
}
fmt.Printf("%+v", dbs)
}
注意正确的标签语法(例如 json:"url"
).
Note the proper tag syntax (e.g. json:"url"
).
输出(在 Go Playground 上试试):
Output (try it on the Go Playground):
[map[db:{URL:mongodb://localhost Port:27000 Uname: Pass: Authdb: Replicas:[map[rs01:{URL:mongodb://localhost Port:27001 Uname: Pass:Authdb: Replicas:[]}] map[rs02:{URL:mongodb://localhost Port:27002 Uname: Pass: Authdb: Replicas:[]}]]}]]
请注意,您可以对始终为 "db"
的第一级进一步建模,我们可以切换到指针(我在第一个示例中使用了非指针,因此打印的结果是可读的):
Note that you can further model the first level which is always "db"
, and we can switch to pointers (I used non-pointers in the first example so the printed result is readable):
type DBReplicated struct {
DB *DBS `json:"db"`
}
type DBS struct {
URL string `json:"url"`
Port string `json:"port"`
Uname string `json:"uname"`
Pass string `json:"pass"`
Authdb string `json:"authdb"`
Replicas []map[string]*DBS `json:"replicas"`
}
func main() {
var dbs []*DBReplicated
if err := json.Unmarshal([]byte(src), &dbs); err != nil {
panic(err)
}
db := dbs[0].DB
fmt.Printf("%+v
", db)
for _, dbs := range db.Replicas {
for name, replica := range dbs {
fmt.Printf("%s: %+v
", name, replica)
}
}
}
输出(在 Go Playground 上试试):
Output (try it on the Go Playground):
&{URL:mongodb://localhost Port:27000 Uname: Pass: Authdb: Replicas:[map[rs01:0x10538200] map[rs02:0x10538240]]}
rs01: &{URL:mongodb://localhost Port:27001 Uname: Pass: Authdb: Replicas:[]}
rs02: &{URL:mongodb://localhost Port:27002 Uname: Pass: Authdb: Replicas:[]}
这篇关于将 json 字符串解组为具有结构本身的一个元素的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!