在不知道结构的情况下解组嵌套 json
unmarshal nested json without knowing structure
我使用键值存储作为我的 golang 应用程序的后端,日期作为键(以保持条目排序)和 json 文档作为值。 json (foo
) 和 type
和 date
的顶级命名空间存在于我正在存储的每个 json 文档中,但除此之外还有一些差异(特别是关于一些嵌套的 json 数据),所以当我从数据库中提取 key 时,我真的不知道我在循环遍历的任何时候提取的是什么。这是 json 数据
的示例
{
"foo": {
"id": "124",
"type": "baz",
"rawdata": [
123,
345,
345
],
"epoch": "1433120656704"
}
}
{
"foo": {
"id": "234",
"type": "bar",
"rawdata": [
{
"key": "dog",
"values": [
123,
234
]
},
{
"key": "cat",
"values": [
23,
45
]
}
],
"epoch": "1433120656705"
}
}
当我从数据库中提取数据时,我做的第一件事是将每个条目解组到 map[string]*json.RawMessage
中以处理 foo
命名空间
//as I'm looping through the entries in the database
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(dbvalue, &objmap); err !=nil{
return err
}
多亏了这个SO answer
但是,与 SO 答案不同的是,当我必须再次解组 foo
命名空间下包含的任何内容时,我不知道要解组到哪个结构中
if err :=json.Unmarshal(*objmap["foo"], &bazorbar; err != nil{
return err
}
type Baz struct{
Id string `json:"id"`
Type string `json:"type"`
RawData []int `json:"rawdata"`
Epoch string `json:"epoch"`
}
type Bar struct{
Id string `json:"id"`
Type string `json:"type"`
RawData []*Qux `json:"rawdata"`
Epoch string `json:"epoch"`
}
//nested inside Bar
type Qux struct{
Key string `json:"key"`
Values []int `json:"values`
}
两部分问题:
- 有没有办法避免重复解组(或者这是我根本不应该关心的事情)
- 我如何找出将 json.RawMessage 解组到哪个结构(这也允许嵌套 json 数据)
更新:@chendesheng 提供的初始答案使我能够找出类型,但一旦确定该类型(我需要做),所以根据对 his/her 答案的评论中的对话,我会对这些可能性中的任何一种感兴趣
a) 制作 json.RawMessage 的副本,如您所示解编组到界面中(由 chendesheng 的回答),然后一旦知道类型(从解组到界面)?
b) 使用正则表达式确定类型,然后在已知类型后解组为该类型的结构
检查结构类型的两种方法:
- 解组 json.RawMessage 到映射[string]接口{}
- 使用正则表达式提取字符串类型
我使用键值存储作为我的 golang 应用程序的后端,日期作为键(以保持条目排序)和 json 文档作为值。 json (foo
) 和 type
和 date
的顶级命名空间存在于我正在存储的每个 json 文档中,但除此之外还有一些差异(特别是关于一些嵌套的 json 数据),所以当我从数据库中提取 key 时,我真的不知道我在循环遍历的任何时候提取的是什么。这是 json 数据
{
"foo": {
"id": "124",
"type": "baz",
"rawdata": [
123,
345,
345
],
"epoch": "1433120656704"
}
}
{
"foo": {
"id": "234",
"type": "bar",
"rawdata": [
{
"key": "dog",
"values": [
123,
234
]
},
{
"key": "cat",
"values": [
23,
45
]
}
],
"epoch": "1433120656705"
}
}
当我从数据库中提取数据时,我做的第一件事是将每个条目解组到 map[string]*json.RawMessage
中以处理 foo
命名空间
//as I'm looping through the entries in the database
var objmap map[string]*json.RawMessage
if err := json.Unmarshal(dbvalue, &objmap); err !=nil{
return err
}
多亏了这个SO answer
但是,与 SO 答案不同的是,当我必须再次解组 foo
命名空间下包含的任何内容时,我不知道要解组到哪个结构中
if err :=json.Unmarshal(*objmap["foo"], &bazorbar; err != nil{
return err
}
type Baz struct{
Id string `json:"id"`
Type string `json:"type"`
RawData []int `json:"rawdata"`
Epoch string `json:"epoch"`
}
type Bar struct{
Id string `json:"id"`
Type string `json:"type"`
RawData []*Qux `json:"rawdata"`
Epoch string `json:"epoch"`
}
//nested inside Bar
type Qux struct{
Key string `json:"key"`
Values []int `json:"values`
}
两部分问题:
- 有没有办法避免重复解组(或者这是我根本不应该关心的事情)
- 我如何找出将 json.RawMessage 解组到哪个结构(这也允许嵌套 json 数据)
更新:@chendesheng 提供的初始答案使我能够找出类型,但一旦确定该类型(我需要做),所以根据对 his/her 答案的评论中的对话,我会对这些可能性中的任何一种感兴趣
a) 制作 json.RawMessage 的副本,如您所示解编组到界面中(由 chendesheng 的回答),然后一旦知道类型(从解组到界面)?
b) 使用正则表达式确定类型,然后在已知类型后解组为该类型的结构
检查结构类型的两种方法:
- 解组 json.RawMessage 到映射[string]接口{}
- 使用正则表达式提取字符串类型