简体   繁体   English

Golang JSON解组多个调用

[英]Golang JSON Unmarshal multiple calls

I'm writing an API wrapper where a call to the API returns a json response of some data, lets say: 我正在编写一个API包装器,其中对API的调用返回一些数据的json响应,可以这样说:

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

for the url example.com/api/groups 对于网址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: 然后,您可以将fields=members附加到URL(例如: example.com/api/groups?group_id=123&fields=members fields=members 123& fields=members )以获取:

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

Note how the other fields are now missing... 请注意现在其他字段是如何丢失的...

Well I'm trying to use a single struct which would look like: 好吧,我正在尝试使用如下所示的单个struct

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

This is what the structs look like: 结构如下所示:

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() {...}

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. 因此,我进行了第一个使用getGroup解组的调用,因此该结构具有良好的GroupIdGroupName ,而Members为空,因为该调用未返回任何内容。

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. 然后我打电话club.GetMembers()来填充Members领域,使整个结构将被填充,但它似乎并没有被提取入结构,因为最终在Members仍然是空的,为数据GroupIdGroupName是还在那里。

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? 我肯定知道该呼叫返回的是我所期望的,所以我认为Unmarshal无法正常工作,那么我该如何处理? Is this not within the functionality of Unmarshal ? 这不是Unmarshal的功能吗?

EDIT I just pushed the exact code to github, still unsure. 编辑我只是将确切的代码推送到github,仍然不确定。

This is the repo: https://github.com/PeteJodo/gosun 这是仓库: https : //github.com/PeteJodo/gosun

This is a gist using the above repo: https://gist.github.com/PeteJodo/d5335b9f66304148483b 这是使用上述仓库的要点: https : //gist.github.com/PeteJodo/d5335b9f66304148483b

The main files of concern: 需要关注的主要文件:

service.go service.go

congress.go congress.go

committees.go Committees.go

legislators.go legislators.go

Alright so my issue has nothing to do with Unmarshal per say. 好的,所以我的问题与Unmarshal无关。 What was happening was that the API response returns: 发生的事情是API响应返回:

{
   results: [{
          group_id: 123,
          group_name: "cool kids"
       }, ...],
   ...
}

...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 我的问题是每个组都是它自己的结构,并且我有一个用于Group结构的方法,该方法可以进行下一个扩展其Members字段的调用,而我正在使用Group结构作为Unmarshal的目的地,而不是诸如Results这样的Results正确构造API响应,然后提取正确的Group及其Members字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM