英文:
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
进行解组,所以结构体中的GroupId
和GroupName
是正确的,而Members
是空的,因为调用没有返回任何内容。
然后,我调用club.GetMembers()
来填充Members
字段,以便整个结构体都被填充,但似乎它没有将其提取到结构体中,因为最后Members
仍然是空的,而GroupId
和GroupName
的数据仍然存在。
我确定调用返回了我期望的内容,所以我认为是Unmarshal
不起作用,那么我该如何解决这个问题?Unmarshal
的功能范围之外吗?
编辑
我刚刚将完整的代码推送到了GitHub,但仍然不确定。
这是存储库:https://github.com/PeteJodo/gosun
这是使用上述存储库的gist:https://gist.github.com/PeteJodo/d5335b9f66304148483b
相关的主要文件:
英文:
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:"group_id"`
GroupName string `json:"group_name"`
Members []struct {...} `json:"members"
}
</s>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.
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:
答案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: "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论