Golang JSON解析多次调用Unmarshal函数

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

Golang JSON Unmarshal multiple calls

问题

我正在编写一个API包装器,其中对API的调用返回一些数据的JSON响应,假设如下:

{
    group_id: 123,
    group_name: "cool kids",
}

对于URL example.com/api/groups

然后,您可以在URL后附加fields=members(例如:example.com/api/groups?group_id=123&fields=members),然后获取如下数据:

{
    members: [...一些数据...]
}

请注意,其他字段现在已经丢失了...

这是结构体的样子:

type Committee struct {
    GroupId   string `json:"group_id"`
    GroupName string `json:"group_name"`
    Members   []struct {
        Person Person  `json:"person"`
        Rank   float64 `json:"rank"`
        Side   string  `json:"side"`
        Title  string  `json:"title"`
    } `json:"members"`
}

type Person struct {
    ID   string `json:"id"`
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func getGroup() Club {...}
func (c *Club) GetMembers() {...}

我首先调用getGroup进行解组,所以结构体中的GroupIdGroupName是正确的,而Members是空的,因为调用没有返回任何内容。

然后,我调用club.GetMembers()来填充Members字段,以便整个结构体都被填充,但似乎它没有将其提取到结构体中,因为最后Members仍然是空的,而GroupIdGroupName的数据仍然存在。

我确定调用返回了我期望的内容,所以我认为是Unmarshal不起作用,那么我该如何解决这个问题?Unmarshal的功能范围之外吗?

编辑
我刚刚将完整的代码推送到了GitHub,但仍然不确定。

这是存储库:https://github.com/PeteJodo/gosun

这是使用上述存储库的gist:https://gist.github.com/PeteJodo/d5335b9f66304148483b

相关的主要文件:

service.go

congress.go

committees.go

legislators.go

英文:

I'm writing an API wrapper where a call to the API returns a json response of some data, lets say:

{
    group_id: 123,
    group_name: "cool kids",
}

for the url example.com/api/groups

You can then append fields=members to the url (so something like: example.com/api/groups?group_id=123&fields=members) to then get:

{
    members: [...some data..]
}

Note how the other fields are now missing...

<s>Well I'm trying to use a single struct which would look like:

type Club struct {
    GroupId int `json:&quot;group_id&quot;`
    GroupName string `json:&quot;group_name&quot;`
    Members []struct {...} `json:&quot;members&quot;
}

</s>This is what the structs look like:

type Committee struct {
    GroupId     string `json:&quot;group_id&quot;`
    GroupName   string `json:&quot;group_name&quot;`
    Members     []struct {
	    Person     Person  `json:&quot;person&quot;`
	    Rank       float64 `json:&quot;rank&quot;`
	    Side       string  `json:&quot;side&quot;`
	    Title      string  `json:&quot;title&quot;`
    } `json:&quot;members&quot;`
}

type Person struct {
    id     string `json:&quot;id&quot;`
    name   string `json:&quot;name&quot;`
    age    int    `json:&quot;age&quot;`
}

func getGroup() Club {...}
func (c *Club) GetMembers() {...}

So I make the first call which unmarshals using getGroup so the struct has GroupId and GroupName just fine and Members is empty because the call didn't return anything for it.

I then call club.GetMembers() to populate the Members field so that the entire struct would be populated but it doesn't seem to be extracting it into the struct because at the end Members is still empty and the data for GroupId and GroupName is still there.

I know for sure that the call is returning what I'm expecting so I figure it's Unmarshal that isn't working so how would I go about this? Is this not within the functionality of Unmarshal?

EDIT
I just pushed the exact code to github, still unsure.

This is the repo: https://github.com/PeteJodo/gosun

This is a gist using the above repo: https://gist.github.com/PeteJodo/d5335b9f66304148483b

The main files of concern:

service.go

congress.go

committees.go

legislators.go

答案1

得分: 0

好的,以下是翻译好的内容:

好的,我的问题与Unmarshal本身无关。问题出在API的响应返回了以下内容:

{
   results: [{
          group_id: 123,
          group_name: "酷炫小孩"
       }, ...],
   ...
}

...每个结果都是一个独立的组。我的问题是,每个组都是一个单独的结构体,而我有一个Group结构体的方法,该方法会调用下一个方法来扩展其Members字段,而我使用Group结构体作为Unmarshal的目标,而不是像Results这样的结构体,后者可以正确地对应API的响应,然后提取正确的Group及其Members字段。

英文:

Alright so my issue has nothing to do with Unmarshal per say. What was happening was that the API response returns:

{
   results: [{
          group_id: 123,
          group_name: &quot;cool kids&quot;
       }, ...],
   ...
}

...each result being an individual group. My problem was that each group was it's own struct and I had a method for the Group struct that would make the next call to expand its Members field, and I was using the Group struct as the destination for Unmarshal instead of something like Results which would be structured correctly for the API response and THEN extract the correct Group and its Members field

huangapple
  • 本文由 发表于 2015年7月11日 06:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/31351553.html
匿名

发表评论

匿名网友

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

确定