在Golang中将JSON映射为结构体的地图。

huangapple go评论78阅读模式
英文:

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"`
}

huangapple
  • 本文由 发表于 2017年3月16日 05:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/42820620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定