Arrays GO中的嵌套数组
我正在尝试将以下JSON映射到go中的Arrays GO中的嵌套数组,arrays,json,go,Arrays,Json,Go,我正在尝试将以下JSON映射到go中的map[string]接口{}: { "Container": { "destinationPath": "/path", "volumeMountPath": "/data", "token": "token", "URL":
map[string]接口{}
:
{
"Container": {
"destinationPath": "/path",
"volumeMountPath": "/data",
"token": "token",
"URL": "https://someurl.com",
"ids": [{
"id": "2322",
"version": "878475"
},
{
"id": "66474",
"version": "6647"
}
]
}
}
到目前为止,我已经:
Data: map[string]interface{}{
"Container": struct {
destinationPath string
volumeMountPath string
token string
URL string
}{"/path",
"/data",
"someToken",
"https://someurl.com"},
},
但我不确定如何表示ID数组
编辑:
这就是我到目前为止所拥有的,设置了ID,但没有设置destinationPath、volumeMountPath和token
Data: map[string]interface{}{
"Container": struct {
destinationPath string
volumeMountPath string
token string
URL string
}{"/path",
"/data",
"someToken",
"https://someurl.com"},
},
"ids": []map[string]interface{}{
map[string]interface{}{
`id`:"3q423442",
`version`:"325435355",
},
},
},
谢谢这似乎可以做到:
package main
import "fmt"
type (
a []interface{}
m map[string]interface{}
)
func main() {
n := m{
"Container": m{
"URL": "https://someurl.com",
"destinationPath": "/path",
"token": "token",
"volumeMountPath": "/data",
"ids": a{
m{"id": "2322", "version": "878475"},
m{"id": "66474", "version": "6647"},
},
},
}
fmt.Println(n)
}
为什么不直接通过json解析器传递呢?有没有理由不将其传递到结构中?像这里一样,在解组JSON数据时,不会设置
token
和destinationPath
等字段。为此,它们需要出口。ids
字段的类型应为[]ID
,其中ID是一个具有ID
和version
字段的结构。这不是嵌套数组,而是对象的数组/切片。另外:展示你已经尝试过的,这是获得你需要的帮助所必需的。谢谢Elias,非常正确,我会更新以展示我所拥有的。使用上面的编辑,ID也是正确的,它们是唯一设置的值。