How do you unmarshal a JSON object to a Golang struct when the JSON field key is a date?

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

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}

huangapple
  • 本文由 发表于 2021年7月13日 04:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/68353565.html
匿名

发表评论

匿名网友

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

确定