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

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

Adding elements to JSON dynamically from a Go struct

问题

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

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

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

type actors struct {
    Data [][]interface{} `json:"data"`
}

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

也就是说:

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

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

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

numberOfActCnt := 0
numberOfColParsed := 0
numberOfColumns := 3

var act actors
var res []actors

for i := 0; i < 2; i++ {
    numberOfActCnt = numberOfActCnt + numberOfColParsed
    for j := 0; j < 1; j++ {
        act = actors{[][]interface{}{{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}}}}
        re, _ := json.Marshal(act)
        fmt.Println(string(re))
        numberOfColParsed = numberOfColumns - 1
    }
}

这给我输出了:

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

如何以以下格式输出:

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

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

英文:

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

{&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:

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

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:

numberOfActCnt := 0
numberOfColParsed := 0
numberOfColumns := 3

var act actors
var res []actors

	for i := 0; i &lt; 2; i++ {
		numberOfActCnt = numberOfActCnt + numberOfColParsed
		for j := 0; j &lt; 1; j++ {
			act = actors{[][]interface{}{{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}}}}
            re, _ := json.Marshal(act)
			fmt.Println(string(re))
			numberOfColParsed = numberOfColumns - 1
		}
	}

This gives me the output:

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

How do I get the output in the format? :

{&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切片中,然后在循环之后,对结果进行编组。

numberOfActCnt := 0
numberOfColParsed := 0
numberOfColumns := 3

var res struct { Data [][]any `json:"data"` }
for i := 0; i < 2; i++ {
	numberOfActCnt = numberOfActCnt + numberOfColParsed
	for j := 0; j < 1; j++ {
		res.Data = append(res.Data, []any{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}})
		numberOfColParsed = numberOfColumns - 1
	}
}
re, err := json.Marshal(res)
if err != nil {
	panic(err)
}
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.

numberOfActCnt := 0
numberOfColParsed := 0
numberOfColumns := 3

var res struct { Data [][]any `json:&quot;data&quot;` }
for i := 0; i &lt; 2; i++ {
	numberOfActCnt = numberOfActCnt + numberOfColParsed
	for j := 0; j &lt; 1; j++ {
		res.Data = append(res.Data, []any{i, []string{rec.records[numberOfActCnt].value, rec.records[numberOfActCnt+1].value}})
		numberOfColParsed = numberOfColumns - 1
	}
}
re, err := json.Marshal(res)
if err != nil {
	panic(err)
}
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:

确定