[英]map[string] struct inside struct
I have a JSON file that looks like this: 我有一个看起来像这样的JSON文件:
{
"jailbreaks": [
{
"jailbroken": false,
"name": "",
"version": "",
"url": "",
"anleitung": [],
"ios": {
"start": "10.2.1"
},
"caveats": "",
"platforms": []
},
{
"jailbroken": true,
"name": "Yalu102",
"version": "beta 6",
"url": "https://domain-dl.tld",
"anleitung": [
{ "blog": "title", "link": "http://domain.tld/" },
{ "blog": "Test", "link": "http://google.at" }
],
"ios": {
"start": "10.2"
},
"caveats": "some text here",
"platforms": [
"Windows",
"OS X",
"Linux"
]
},
And I create the object to work with like this: 我创建了可以使用的对象,如下所示:
type Jailbreak struct {
Jailbroken bool `json:"jailbroken"`
Name string `json:"name"`
Version string `json:"version"`
URL string `json:"url"`
Anleitung map[string]struct {
Name string `json:"blog"`
Link string `json:"link"`
} `json:"anleitung"`
Firmwares struct {
Start string `json:"start"`
End string `json:"end"`
} `json:"ios"`
Platforms []string `json:"platforms"`
Caveats string `json:"caveats"`
}
When I want to build my go program I get an error, that the JSON file cannot be read. 当我想构建go程序时,出现错误,无法读取JSON文件。 But as soon as I delete the
map[string]struct
I can compile and run the program without any error and everything works fine. 但是,只要删除
map[string]struct
我就可以编译并运行该程序,而不会出现任何错误,并且一切正常。 Am I messing around with something or is there an error in my JSON file? 我是在弄乱东西还是JSON文件中有错误?
The json provided is not valid (as the array does not have a closing ]
and the top level json object lacks another closing }
) so let's assume it's like: 提供的JSON是不是有效的(如在阵列不具有闭合
]
和顶层JSON对象缺乏另一个闭合}
),以便让我们假设它是这样的:
{
"jailbreaks": [
{
"jailbroken": false,
"name": "",
"version": "",
"url": "",
"anleitung": [],
"ios": {
"start": "10.2.1",
"end": ""
},
"platforms": [],
"caveats": ""
},
{
"jailbroken": true,
"name": "Yalu102",
"version": "beta 6",
"url": "https://domain-dl.tld",
"anleitung": [
{
"blog": "title",
"link": "http://domain.tld/"
},
{
"blog": "Test",
"link": "http://google.at"
}
],
"ios": {
"start": "10.2",
"end": ""
},
"platforms": [
"Windows",
"OS X",
"Linux"
],
"caveats": "some text here"
}
]
}
The data structure Jailbreaks
(first one), marshals-to/unmarshals-from this json properly: 数据结构
Jailbreaks
(第一个),正确地从此json封送/解封:
type Jailbreaks struct {
List []Jailbreak `json:"jailbreaks"`
}
type Jailbreak struct {
Jailbroken bool `json:"jailbroken"`
Name string `json:"name"`
Version string `json:"version"`
URL string `json:"url"`
Anleitung []struct {
Name string `json:"blog"`
Link string `json:"link"`
} `json:"anleitung"`
Firmwares struct {
Start string `json:"start"`
End string `json:"end"`
} `json:"ios"`
Platforms []string `json:"platforms"`
Caveats string `json:"caveats"`
}
As you see Anleitung
is declared as a slice (not a map). 如您所见,
Anleitung
被声明为切片(不是地图)。
Use omitempty
flag for when your "anleitung" is empty in JSON to be consumed. 当您的“ anleitung”在JSON中为空时,请使用
omitempty
标志。 Beware though, when that is the case, your Jailbreak
struct won't have an "anleitung" field. 但是请注意,在这种情况下,您的
Jailbreak
结构将没有“ anleitung”字段。
Change your map's json flag to to; 将地图的json标志更改为;
Anleitung map[string]struct {
Name string `json:"blog"`
Link string `json:"link"`
} `json:"anleitung,omitempty"`
Option 2; 选项2;
I guess you could also use Anleitung map[string]interface{}
but that is better for "holding a map of strings to arbitrary data types". 我猜您也可以使用
Anleitung map[string]interface{}
但这对于“将字符串映射为任意数据类型”更好。 In your case the data is not arbitrary but rather, empty I guess. 在您的情况下,数据不是任意的,而是空的。 And looks like that is just temporary.
看起来那只是暂时的。
I'd go for option 1, then I'd check if my struct contains any Anleitung
data or not before accessing it. 我会选择选项1,然后在访问它之前检查我的结构是否包含任何
Anleitung
数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.