英文:
Map of Struct from json in Golang
问题
所以我正在尝试将一个JSON解析为一些结构体,以下是可以正常工作的代码:
type train struct {
ID string `json:"id"`
Price float64 `json:"price,string"`
Distance float64 `json:"Distance,string"`
}
type Station struct {
ID int64 `json:"id,string"`
arrTrain []train `json:"arr"`
depTrain []train `json:"dep"`
}
然而,问题在于我希望能够轻松地通过它们的ID引用arrTrain和depTrain中的项,所以我认为我需要将Station结构体更改为具有以ID为键的map的arrTrain和depTrain。在解析JSON时是否可能实现这一点,还是必须在“后处理”中进行?
编辑:
正如评论中所述,不幸的是,我的JSON的格式如下:
{
"id": 1,
"arr": [
{"id": "one", "price": "$10.1", "Distance": "100km"},
{...}
],
"dep": [
{"id": "one", "price": "$10.1", "Distance": "100km"},
{...}
]
}
换句话说,ID不在JSON对象的外部,而arrTrain是一个列表。
英文:
So I am trying to parse a json into some structs and that works ok with the following:
type train struct {
ID string `json:"id"`
Price float64 `json:"price,string"`
Distance float64 `json:"Distance,string"`
}
type Station struct {
ID int64 `json:"id,string"`
arrTrain []train`json:"arr"`
depTrain []train`json:"dep"`
}
The problem, however, is that I would like to easily be able to reference the items in arrTrain and depTrain using their ID, so I think I need to change the Station struct to have arrTrain and depTrain as maps with the ID as the key. Is this possible when parsing the json or does it have to be 'post-processed'?
EDIT:
As stated in one of the comments, unfortunately my json is in the following form:
{
"id":1,
"arr": [
{"id":"one","price":"$10.1","Distance":"100km"},
{...}
],
"dep":[
{"id":"one","price":"$10.1","Distance":"100km"},
{...}
]
}
In other words the ID is not on the outside of the json object and arrTrain is list.
答案1
得分: 2
是的,你可以像这样定义一个站点:
type Station struct {
ID int64 `json:"id,string"`
arrTrain map[string]train `json:"arr"`
depTrain map[string]train `json:"dep"`
}
你的 JSON 应该像这样:
{
"id": 1,
"arr": {
"one": {"id":"one","price":"$10.1","Distance":"100km"},
"two": ...
},
"dep": {
"one": {"id":"one","price":"$10.1","Distance":"100km"},
"two": ...
}
}
请注意,这只是一个示例,你需要根据你的实际需求进行适当的修改。
英文:
Yes, you can define station like this:
type Station struct {
ID int64 `json:"id,string"`
arrTrain map[string]train `json:"arr"`
depTrain map[string]train `json:"dep"`
}
And your JSON should like this
{
"id":1,
"arr": {
"one":{"id":"one","price":"$10.1","Distance":"100km"},
"two":...
},
"dep":{
"one":{"id":"one","price":"$10.1","Distance":"100km"},
"two":...
}
}
答案2
得分: 1
编辑:是的,你可以。(此前版本的回答中提到键只能是字符串,但从1.7版本开始不再是这样)
你可以在这个 playground 中看到实际效果。
此外,正如康纳在评论中指出的那样,你的字段名称必须是可导出的,以便 encoding/json 能够工作。
type Station struct {
ID int64 `json:"id,string"`
ArrTrain map[string]train `json:"arr"`
DepTrain map[string]train `json:"dep"`
}
英文:
Edit: Yes you can. (an earlier version of this answer stated keys can only be strings, but as of 1.7 this is not true)
You can see this in action with this playground
Also as conner points out in the comments your field names will have to be exportable for encoding/json to work
type Station struct {
ID int64 `json:"id,string"`
ArrTrain map[string]train `json:"arr"`
DepTrain map[string]train `json:"dep"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论