我使用键值存储作为我的golang应用程序的后端,日期用作键(保持条目排序)和json文档作为值.的JSON的顶层名字空间(foo),并且type和date 都存在我存储但除此之外也有一些差异(特别是相对于一些嵌套JSON数据),每个JSON文档中,因此,当keyI'm从数据库中提取,我真的不知道我在循环中随时都会拔出什么东西.以下是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"}}
Run Code Online (Sandbox Code Playgroud)
当我从数据库中取出时,我要做的第一件事就是将每个条目解组为一个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
}
Run Code Online (Sandbox Code Playgroud)
我感谢这个答案
然而,与那个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 …Run Code Online (Sandbox Code Playgroud) go ×1