从Go结构体动态地向JSON中添加元素。

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

Adding elements to JSON dynamically from a Go struct

问题

我想要得到的输出格式如下:

  1. {"data": [ [ 0, ["Brendan", "Fraser"] ] , [ 1, ["Keanu", "Reeves"] ] ] }

为了实现这个目标,我定义了以下结构体:

  1. type actors struct {
  2. Data [][]interface{} `json:"data"`
  3. }

BrendanFraserKeanuReeves这些个体值来自另一个数据结构,可以通过rec.records[0].value这样的格式访问。

也就是说:

rec.records[0].value对应于Brendan

rec.records[1].value对应于Fraser,依此类推。

为了给这个结构体赋值,我这样做:

  1. numberOfActCnt := 0
  2. numberOfColParsed := 0
  3. numberOfColumns := 3
  4. var act actors
  5. var res []actors
  6. for i := 0; i < 2; i++ {
  7. numberOfActCnt = numberOfActCnt + numberOfColParsed
  8. for j := 0; j < 1; j++ {
  9. act = actors{[][]interface{}{{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}}}}
  10. re, _ := json.Marshal(act)
  11. fmt.Println(string(re))
  12. numberOfColParsed = numberOfColumns - 1
  13. }
  14. }

这给我输出了:

  1. {"data":[[0,["Brendan","Fraser"]]]}
  2. {"data":[[1,["Keanu","Reeves"]]]}

如何以以下格式输出:

  1. {"data": [ [ 0, [ "Brendan", "Fraser" ] ] , [ 1, [ "Keanu", "Reeves" ] ] ] }

我认为我的结构体定义是正确的,但是如何将值追加到同一个JSON中呢?

英文:

The output that I want to get is in the format:

  1. {&quot;data&quot;: [ [ 0, [ &quot;Brendan&quot;, &quot;Fraser&quot; ] ] , [ 1, [ &quot;Keanu&quot;, &quot;Reeves&quot; ] ] ] }

To do this, I have defined the following struct:

  1. type actors struct {
  2. Data [][]interface{} `json:&quot;data&quot;`
  3. }

The individual values Brendan , Fraser and Keanu, Reeves are coming from another data struct that is accessed in the format rec.records[0].value .

That is:

rec.records[0].value would correspond to Brendan

rec.records[1].value would correspond to Fraser and so on.

To assign values to this struct I am doing:

  1. numberOfActCnt := 0
  2. numberOfColParsed := 0
  3. numberOfColumns := 3
  4. var act actors
  5. var res []actors
  6. for i := 0; i &lt; 2; i++ {
  7. numberOfActCnt = numberOfActCnt + numberOfColParsed
  8. for j := 0; j &lt; 1; j++ {
  9. act = actors{[][]interface{}{{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}}}}
  10. re, _ := json.Marshal(act)
  11. fmt.Println(string(re))
  12. numberOfColParsed = numberOfColumns - 1
  13. }
  14. }

This gives me the output:

  1. {&quot;data&quot;:[[0,[&quot;Brendan&quot;,&quot;Fraser&quot;]]]}
  2. {&quot;data&quot;:[[1,[&quot;Keanu&quot;,&quot;Reeves&quot;]]]}

How do I get the output in the format? :

  1. {&quot;data&quot;: [ [ 0, [ &quot;Brendan&quot;, &quot;Fraser&quot; ] ] , [ 1, [ &quot;Keanu&quot;, &quot;Reeves&quot; ] ] ] }

I think my struct is defined correctly, however how do I append values to the same json?

答案1

得分: 1

首先,循环遍历所有记录,并使用append将它们存储在Data [][]any切片中,然后在循环之后,对结果进行编组。

  1. numberOfActCnt := 0
  2. numberOfColParsed := 0
  3. numberOfColumns := 3
  4. var res struct { Data [][]any `json:"data"` }
  5. for i := 0; i < 2; i++ {
  6. numberOfActCnt = numberOfActCnt + numberOfColParsed
  7. for j := 0; j < 1; j++ {
  8. res.Data = append(res.Data, []any{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}})
  9. numberOfColParsed = numberOfColumns - 1
  10. }
  11. }
  12. re, err := json.Marshal(res)
  13. if err != nil {
  14. panic(err)
  15. }
  16. fmt.Println(string(re))

注意:any是自Go1.18以来interface{}的别名,如果你使用的是旧版本的Go,则需要继续使用interface{}

英文:

First, loop over all the records and store them inside the Data [][]any slice using append and then, after the loop, marshal the result.

  1. numberOfActCnt := 0
  2. numberOfColParsed := 0
  3. numberOfColumns := 3
  4. var res struct { Data [][]any `json:&quot;data&quot;` }
  5. for i := 0; i &lt; 2; i++ {
  6. numberOfActCnt = numberOfActCnt + numberOfColParsed
  7. for j := 0; j &lt; 1; j++ {
  8. res.Data = append(res.Data, []any{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}})
  9. numberOfColParsed = numberOfColumns - 1
  10. }
  11. }
  12. re, err := json.Marshal(res)
  13. if err != nil {
  14. panic(err)
  15. }
  16. fmt.Println(string(re))

Note: any is an alias for interface{} since Go1.18, if you are on an older version of Go then you'll need to keep using interface{}.

huangapple
  • 本文由 发表于 2023年3月17日 23:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75769274.html
匿名

发表评论

匿名网友

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

确定