Golang将JSON映射到结构体

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

Golang map json to struct

问题

我有一个JSON,我需要使用一个结构体来提取其中的数据:

我试图将其映射到以下结构体:

  1. type Message struct {
  2. Name string `json:"name"`
  3. Values []struct {
  4. Value int `json:"value,omitempty"`
  5. Comments int `json:"comments,omitempty"`
  6. Likes int `json:"likes,omitempty"`
  7. Shares int `json:"shares,omitempty"`
  8. } `json:"values"`
  9. }

这是我的JSON:

  1. [{
  2. "name": "organic_impressions_unique",
  3. "values": [{
  4. "value": 8288
  5. }]
  6. }, {
  7. "name": "post_story_actions_by_type",
  8. "values": [{
  9. "shares": 234,
  10. "comments": 838,
  11. "likes": 8768
  12. }]
  13. }]

我的问题是:

  1. 如何构建我的结构体?
  2. 如何读取name、values和comments?

到目前为止,我无法使用以下代码读取数据:

  1. msg := []Message{}
  2. getJson("https://json.url", msg)
  3. println(msg[0])

getJson函数如下:

  1. func getJson(url string, target interface{}) error {
  2. r, err := myClient.Get(url)
  3. if err != nil {
  4. return err
  5. }
  6. defer r.Body.Close()
  7. return json.NewDecoder(r.Body).Decode(target)
  8. }
英文:

I have a JSON which I need to extract the data out of it using a struct:

I am trying to map it to the below struct:

  1. type Message struct {
  2. Name string `json:"name"`
  3. Values []struct {
  4. Value int `json:"value,omitempty"`
  5. Comments int `json:"comments,omitempty"`
  6. Likes int `json:"likes,omitempty"`
  7. Shares int `json:"shares,omitempty"`
  8. } `json:"values"`
  9. }

This is my json:

  1. [{
  2. "name": "organic_impressions_unique",
  3. "values": [{
  4. "value": 8288
  5. }]
  6. }, {
  7. "name": "post_story_actions_by_type",
  8. "values": [{
  9. "shares": 234,
  10. "comments": 838,
  11. "likes": 8768
  12. }]
  13. }]

My questions are:

  1. How to structure my struct?
  2. How to read the name, values and comments?

So far I couldn't read the data using the below code:

  1. msg := []Message{}
  2. getJson("https://json.url", msg)
  3. println(msg[0])

the getJson function:

  1. func getJson(url string, target interface{}) error {
  2. r, err := myClient.Get(url)
  3. if err != nil {
  4. return err
  5. }
  6. defer r.Body.Close()
  7. return json.NewDecoder(r.Body).Decode(target)
  8. }

答案1

得分: 16

你的结构是正确的。你只需要使用json.Unmarshal函数和一个正确的目标对象(Message实例的切片:[]Message{})来解析JSON数据。

正确的解析

  1. type Message struct {
  2. Name string `json:"name"`
  3. Values []struct {
  4. Value int `json:"value,omitempty"`
  5. Comments int `json:"comments,omitempty"`
  6. Likes int `json:"likes,omitempty"`
  7. Shares int `json:"shares,omitempty"`
  8. } `json:"values"`
  9. }
  10. func main() {
  11. input := []byte(`
  12. [{
  13. "name": "organic_impressions_unique",
  14. "values": [{
  15. "value": 8288
  16. }]
  17. }, {
  18. "name": "post_story_actions_by_type",
  19. "values": [{
  20. "shares": 234,
  21. "comments": 838,
  22. "likes": 8768
  23. }]
  24. }]
  25. `)
  26. messages := []Message{} // Message实例的切片
  27. json.Unmarshal(input, &messages)
  28. fmt.Println(messages)
  29. }
英文:

Your struct is correct. All you need is <s>love</s> to use json.Unmarshal function with a correct target object which is slice of Message instances: []Message{}

Correct unmarshaling:

  1. type Message struct {
  2. Name string `json:&quot;name&quot;`
  3. Values []struct {
  4. Value int `json:&quot;value,omitempty&quot;`
  5. Comments int `json:&quot;comments,omitempty&quot;`
  6. Likes int `json:&quot;likes,omitempty&quot;`
  7. Shares int `json:&quot;shares,omitempty&quot;`
  8. } `json:&quot;values&quot;`
  9. }
  10. func main() {
  11. input := []byte(`
  12. [{
  13. &quot;name&quot;: &quot;organic_impressions_unique&quot;,
  14. &quot;values&quot;: [{
  15. &quot;value&quot;: 8288
  16. }]
  17. }, {
  18. &quot;name&quot;: &quot;post_story_actions_by_type&quot;,
  19. &quot;values&quot;: [{
  20. &quot;shares&quot;: 234,
  21. &quot;comments&quot;: 838,
  22. &quot;likes&quot;: 8768
  23. }]
  24. }]
  25. `)
  26. messages := []Message{} // Slice of Message instances
  27. json.Unmarshal(input, &amp;messages)
  28. fmt.Println(messages)
  29. }

答案2

得分: 3

你的JSON似乎是一个数组。只需将其解组为切片即可。类似这样的代码:

  1. var messages []Message
  2. err := json.Unmarshal(json, &messages)

应该可以工作。

英文:

Your JSON seems to be an array. Just unmarshall it to a slice. Something like:

  1. var messages []Message
  2. err := json.Unmarshal(json, &amp;messages)

Should work.

答案3

得分: 0

我不知道现在是否有帮助,但我最近编写了一个从JSON输入生成精确Go类型的实用程序:https://github.com/m-zajac/json2go

对于第一个帖子中的JSON,它生成了以下结构体:

  1. type Object struct {
  2. Name string `json:"name"`
  3. Values []struct {
  4. Comments *int `json:"comments,omitempty"`
  5. Likes *int `json:"likes,omitempty"`
  6. Shares *int `json:"shares,omitempty"`
  7. Value *int `json:"value,omitempty"`
  8. } `json:"values"`
  9. }

你可以像这样将数据解码到这个结构体中:

  1. var docs []Object
  2. if err := json.Unmarshal(input, &docs); err != nil {
  3. // 处理错误
  4. }
英文:

I don't know if this will be any help now, but i've recently written utility for generating exact go type from json input: https://github.com/m-zajac/json2go

For json from first post it generates this struct:

  1. type Object struct {
  2. Name string `json:&quot;name&quot;`
  3. Values []struct {
  4. Comments *int `json:&quot;comments,omitempty&quot;`
  5. Likes *int `json:&quot;likes,omitempty&quot;`
  6. Shares *int `json:&quot;shares,omitempty&quot;`
  7. Value *int `json:&quot;value,omitempty&quot;`
  8. } `json:&quot;values&quot;`
  9. }

You can decode data to this struct like this:

  1. var docs []Object
  2. if err := json.Unmarshal(input, &amp;docs); err != nil {
  3. // handle error
  4. }

huangapple
  • 本文由 发表于 2017年2月9日 20:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/42136765.html
匿名

发表评论

匿名网友

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

确定