英文:
Adding elements to JSON dynamically from a Go struct
问题
我想要得到的输出格式如下:
{"data": [ [ 0, ["Brendan", "Fraser"] ] , [ 1, ["Keanu", "Reeves"] ] ] }
为了实现这个目标,我定义了以下结构体:
type actors struct {
Data [][]interface{} `json:"data"`
}
Brendan
、Fraser
、Keanu
和Reeves
这些个体值来自另一个数据结构,可以通过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:
{"data": [ [ 0, [ "Brendan", "Fraser" ] ] , [ 1, [ "Keanu", "Reeves" ] ] ] }
To do this, I have defined the following struct:
type actors struct {
Data [][]interface{} `json:"data"`
}
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 < 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
}
}
This gives me the output:
{"data":[[0,["Brendan","Fraser"]]]}
{"data":[[1,["Keanu","Reeves"]]]}
How do I get the output in the format? :
{"data": [ [ 0, [ "Brendan", "Fraser" ] ] , [ 1, [ "Keanu", "Reeves" ] ] ] }
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:"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))
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{}
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论