英文:
How do you unmarshal a JSON object to a Golang struct when the JSON field key is a date?
问题
以下是将此JSON响应解组为Golang结构的最佳方法。由于结构字段将随日期的变化而不同,因此无法使用自动生成的结构。可以使用map[string]map[string]int的结构来动态处理此JSON数据。
type Response struct {
Data map[string]map[string]int `json:"data"`
}
然后,您可以使用json.Unmarshal函数将JSON数据解组到Response结构中:
jsonData := []byte(`{
"data": {
"2021-07-12": {
"Neutral": 3,
"Positive": 4,
"Negative": 4
},
"2021-07-11": {
"Neutral": 0,
"Positive": 1,
"Negative": 4
},
"2021-07-10": {
"Neutral": 0,
"Positive": 0,
"Negative": 3
}
}
}`)
var response Response
err := json.Unmarshal(jsonData, &response)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println(response)
这样,您就可以动态地访问和处理不同日期的数据了。
英文:
I have the following JSON response. What would be the best way to unmarshal this into a Golang struct? The JSON to Golang autogenerated struct is saying the named properties of the struct should be 20210712, 20210711, 20210710, etc. but that won't work because the struct field will be different as the dates change in the future. What would be the best way to do this dynamically?
{
"data": {
"2021-07-12": {
"Neutral": 3,
"Positive": 4,
"Negative": 4
},
"2021-07-11": {
"Neutral": 0,
"Positive": 1,
"Negative": 4
},
"2021-07-10": {
"Neutral": 0,
"Positive": 0,
"Negative": 3
}
}
}
答案1
得分: 2
你可以使用一个map:
type Item struct {
Neutral int
Positive int
Negative int
}
type Data struct {
Data map[string]Item `json:"data"`
}
当你解析时,你可以使用data.Data["2021-07-11"]
。
英文:
You can use a map:
type Item struct {
Neutral int
Positive int
Negative int
}
type Data struct {
Data map[string]Item `json:"data"`
}
When you unmarshal, you can use data.Data["2021-07-11"]
答案2
得分: 1
根据Burak Serdar的输入,我为您的情景创建了一个简单的程序,如下所示:
package main
import (
"encoding/json"
"fmt"
)
type Item struct {
Neutral int
Positive int
Negative int
}
type Data struct {
Data map[string]Item `json:"data"`
}
func main() {
var resData Data
var data = []byte(`{
"data":{
"2021-07-12":{
"Neutral":3,
"Positive":4,
"Negative":4
},
"2021-07-11":{
"Neutral":0,
"Positive":1,
"Negative":4
},
"2021-07-10":{
"Neutral":0,
"Positive":0,
"Negative":3
}
}
}`)
if err := json.Unmarshal(data, &resData); err != nil {
panic(err)
}
fmt.Println(resData)
fmt.Println(resData.Data["2021-07-10"])
}
输出结果:
{map[2021-07-10:{0 0 3} 2021-07-11:{0 1 4} 2021-07-12:{3 4 4}]}
{0 0 3}
英文:
Based on the input from Burak Serdar ,I created a simple program for your scenario as follows:
package main
import (
"encoding/json"
"fmt"
)
type Item struct {
Neutral int
Positive int
Negative int
}
type Data struct {
Data map[string]Item `json:"data"`
}
func main() {
var resData Data
var data = []byte(`{
"data":{
"2021-07-12":{
"Neutral":3,
"Positive":4,
"Negative":4
},
"2021-07-11":{
"Neutral":0,
"Positive":1,
"Negative":4
},
"2021-07-10":{
"Neutral":0,
"Positive":0,
"Negative":3
}
}
}`)
if err := json.Unmarshal(data, &resData); err != nil {
panic(err)
}
fmt.Println(resData)
fmt.Println(resData.Data["2021-07-10"])
}
Output:
{map[2021-07-10:{0 0 3} 2021-07-11:{0 1 4} 2021-07-12:{3 4 4}]}
{0 0 3}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论