英文:
Processing a JSON API Response
问题
我正在处理我的第一个Go项目,对于一些复杂的数据结构还有些困惑。目前,我正在模拟一个API的响应,该API将返回一个包含两个属性的JSON对象:
columns
包含按顺序处理的字段数组data
包含一个包含在columns
中列出的属性的对象数组
我需要遍历 data
数组/切片,并在其中遍历 columns
数组/切片,并从当前数据对象中提取当前属性。我没有正确处理数据结构。经过多次尝试和错误,我得到了以下代码,并且足够沮丧,决定提出问题。
res, err := http.Get("http://roadmap-proto.robwilkerson.org/demo.json")
failOnError(err, "Uh oh")
defer res.Body.Close()
// 解码和处理JSON响应
// var v map[string]interface{}
type View struct {
Columns []string `json:"columns"`
Data map[string]interface{} `json:"data"`
}
v := View{}
json.NewDecoder(res.Body).Decode(&v)
log.Printf("[====>] Data: %s", v)
log.Printf()
的输出如下:
Data: {[id avatar name email phone address] map[]}
columns
似乎读取正常,但是数据不是。最终,我需要循环遍历数据,并将每个字段写入Excel单元格,但到目前为止,我甚至无法正确地“加载”数据。
非常感谢任何对正确方向的推动。
英文:
Working on my first Go project and I'm having some trouble wrapping my mind around some of the complex data structures. At the moment, I'm mocking up the response from an API that will return a JSON object containing 2 properties:
columns
contains an array of fields in the order they should be handleddata
contains an array of objects containing the properties listed incolumns
I need to iterate over the data
array/slice and, within that, iterate over the columns
array/slice and extract the current property from the current data object. I'm not handling the data structure properly. A lot of trial and error has left me with the following code and enough frustration to decide I simply needed to ask the question.
res, err := http.Get("http://roadmap-proto.robwilkerson.org/demo.json")
failOnError(err, "Uh oh")
defer res.Body.Close()
// Decode and process the JSON response
// var v map[string]interface{}
type View struct {
Columns []string `"json:columns"`
Data map[string]interface{} `"json:data"`
}
v := View{}
json.NewDecoder(res.Body).Decode(&v)
log.Printf("[====>] Data: %s", v)
The output from log.Printf()
is this:
Data: {[id avatar name email phone address] map[]}
The columns
seems to read okay, but not the data. Ultimately I need to loop over that and write each field to an Excel cell, but so far I can't even get the data to "load" properly.
Any push in the right direction would be much appreciated.
UPDATE
I inadvertently omitted some key info: On any given call to the API, the number and names of the columns may be different. Within a given call, all objects in the data
array will be the same, but each call could be very, very different. I can't define a static struct
and have that work.
答案1
得分: 0
我会为你翻译以下内容:
我会使用两个结构体来实现这个:
type ColData struct {
Id string `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Address string `json:"address"`
}
type View struct {
Columns []string `json:"columns"`
Data []ColData `json:"data"`
}
对于你的更新,只需要将
type View struct {
Columns []string `json:"columns"`
Data map[string]interface{} `json:"data"`
}
改为
type View struct {
Columns []string `json:"columns"`
Data []map[string]interface{} `json:"data"`
}
以将数据存入结构体中。也许将其作为string
或json.RawMessage
来处理会更好,而不是使用interface{}
,但我不太清楚你的具体需求和可能得到的结果。
英文:
I would use two structs for this:
type ColData struct {
Id string `json:"id"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Address string `json:"address"`
}
type View struct {
Columns []string `json:"columns"`
Data []ColData `json:"data"`
}
In response to your update:
type View struct {
Columns []string `"json:columns"`
Data map[string]interface{} `"json:data"`
}
just needs to become
type View struct {
Columns []string `"json:columns"`
Data []map[string]interface{} `"json:data"`
}
to get your data into the struct. Might also be nice to just work with them as string
or json.RawMessage
instead of an interface{}
as well, but I don't really know what you're doing with it/what you could possibly get out.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论