在Go / Golang中解组嵌套的JSON数组

嗨,我在解组嵌套的JSON数组时遇到问题。 我应该创建什么结构?

我想尽可能避免使用 interface {} </ code>,但我真的不知道在这种情况下是否可以。</ p>

Json我想要 取消编组:</ p>

 “ [[[[{{” aaa \“:\” aaa \“}]]]”“ 
</ code> </ pre>

和我想用来解组的结构:</ p>

  type SomeStructNestedNested struct {
Aaa stringjson:“ aaa”
}
type SomeStructNested struct {
SomeStructNestedNested [] SomeStructNestedNested
}
type SomeStruct struct {
SomeStructNested [] SomeStructNested
}
</ code> </ pre>

链接到示例:
https://play.golang.org/p/owuMptNrix </ p>
</ div>

展开原文

原文

Hi I have problem with unmarshalling nested JSON arrays. What struct should I create? I want to avoid using interface{} as much as possible, but I really don't know if it is possible in this case.

Json I want to unmarshall:

"[[[{\"aaa\": \"aaa\"}]]]"

and struct I want to use to unmarshall this:

type SomeStructNestedNested struct {
   Aaa string `json:"aaa"`
}
type SomeStructNested struct {
   SomeStructNestedNested []SomeStructNestedNested
}
type SomeStruct struct {
   SomeStructNested []SomeStructNested
}

Link to example: https://play.golang.org/p/owuMptNrix

2个回答



是的,答案只是一小部分:</ p>

  type AutoGenerated []  [] [] struct {
Aaa字符串json:“ aaa”
}
</ code> </ pre>

由于您的问题,我发现了一个错误 工具我一直将它用于Go的Json操作,它可以为您节省很多 无聊的时间,最好使用ticks``来表示json字符串,例如此处 </ p>

</ div>

展开原文

原文

Yes the answer is just an slice of slices:

type AutoGenerated [][][]struct {
     Aaa string `json:"aaa"`
}

Well thanks to your question I've discovered a bug in this 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

The problem here is that you're attempting to use structs to represent the nesting when they're actually arrays. I find the form of the json itself to be quite poor but if you're stuck with it then you have to have a 3d array for the unmarshal, using just that 'nested nested' struct type. Below is link to your play example with some crude modifications to demonstrate the point.

type SomeStructNestedNested struct {
    Aaa string `json:"aaa"`
}

jsonString := "[[[{\"aaa\": \"aaa\"}]]]"
d := [][][]SomeStructNestedNested{}
json.Unmarshal([]byte(jsonString), &d)
fmt.Printf("%v", d)

https://play.golang.org/p/88M0_UR_3_

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐