在 Go / Golang 中解组嵌套 JSON 数组
Unmarshall nested JSON Arrays in Go / Golang
您好,我在解组嵌套 JSON 数组时遇到问题。我应该创建什么结构?
我想尽可能避免使用interface{}
,但我真的不知道在这种情况下是否可行。
Json 我要解组:
"[[[{\"aaa\": \"aaa\"}]]]"
我想用来解组的结构:
type SomeStructNestedNested struct {
Aaa string `json:"aaa"`
}
type SomeStructNested struct {
SomeStructNestedNested []SomeStructNestedNested
}
type SomeStruct struct {
SomeStructNested []SomeStructNested
}
Link 举例:
https://play.golang.org/p/owuMptNrix
这里的问题是您试图使用结构来表示嵌套,而实际上它们是数组。我发现 json 本身的形式很差,但如果你坚持使用它,那么你必须有一个用于解组的 3d 数组,只使用 'nested nested' 结构类型。下面是 link 您的游戏示例,并进行了一些粗略的修改以证明这一点。
type SomeStructNestedNested struct {
Aaa string `json:"aaa"`
}
jsonString := "[[[{\"aaa\": \"aaa\"}]]]"
d := [][][]SomeStructNestedNested{}
json.Unmarshal([]byte(jsonString), &d)
fmt.Printf("%v", d)
是的,答案只是一小部分:
type AutoGenerated [][][]struct {
Aaa string `json:"aaa"`
}
好吧,感谢你的问题,我发现了这个 tool I always use it for Json manipulation with Go, it can save you a lot of boring time, also it's better to use ticks `` to represent json strings like here
中的一个错误
您好,我在解组嵌套 JSON 数组时遇到问题。我应该创建什么结构?
我想尽可能避免使用interface{}
,但我真的不知道在这种情况下是否可行。
Json 我要解组:
"[[[{\"aaa\": \"aaa\"}]]]"
我想用来解组的结构:
type SomeStructNestedNested struct {
Aaa string `json:"aaa"`
}
type SomeStructNested struct {
SomeStructNestedNested []SomeStructNestedNested
}
type SomeStruct struct {
SomeStructNested []SomeStructNested
}
Link 举例: https://play.golang.org/p/owuMptNrix
这里的问题是您试图使用结构来表示嵌套,而实际上它们是数组。我发现 json 本身的形式很差,但如果你坚持使用它,那么你必须有一个用于解组的 3d 数组,只使用 'nested nested' 结构类型。下面是 link 您的游戏示例,并进行了一些粗略的修改以证明这一点。
type SomeStructNestedNested struct {
Aaa string `json:"aaa"`
}
jsonString := "[[[{\"aaa\": \"aaa\"}]]]"
d := [][][]SomeStructNestedNested{}
json.Unmarshal([]byte(jsonString), &d)
fmt.Printf("%v", d)
是的,答案只是一小部分:
type AutoGenerated [][][]struct {
Aaa string `json:"aaa"`
}
好吧,感谢你的问题,我发现了这个 tool I always use it for Json manipulation with Go, it can save you a lot of boring time, also it's better to use ticks `` to represent json strings like here
中的一个错误