I'm trying to decode dynamic/random JSON responses in GO, with nested data
我正在尝试使用嵌套数据解码GO中的动态/随机JSON响应
body, _ := ioutil.ReadAll(response.Body)
resp := make(map[string]interface{})
err = json.Unmarshal(body, &resp)
fmt.Printf("BODY: %T<\n", body)
fmt.Printf("BODY: %s<\n", body)
fmt.Printf("RESP: %s<\n", resp)
fmt.Printf("RESP: %T<\n", resp)
fmt.Printf("RESP[results]: %T<\n", resp["results"])
fmt.Printf("RESP[results]: %s<\n", resp["results"])
body is the JSON result from the HTTP server and I unmarshall it and the result looks to be a slice of bytes.
body是来自HTTP服务器的JSON结果,我解组它,结果看起来像是一个字节片。
BODY: []uint8
BODY: {"results":[{"code":500.0,"errors":["Configuration file 'c2-web-2.conf' already exists."],"status":"Object could not be created."}]}
BODY:{“results”:[{“code”:500.0,“errors”:[“配置文件'c2-web-2.conf'已存在。”],“status”:“无法创建对象”。 }]}
So I unmarshall it into resp and that works as expected.
所以我将它解组为resp并且按预期工作。
RESP: map[string]interface {}
RESP:map [string] interface {}
RESP: map[results:[map[code:%!s(float64=500) errors:[Configuration file 'c2-web-2.conf' already exists.] status:Object could not be created.]]]<
RESP:map [结果:[map [code:%!s(float64 = 500)错误:[配置文件'c2-web-2.conf'已经存在。]状态:无法创建对象。]]] <
I'm able to access the map with the key results.
我可以使用关键结果访问地图。
RESP[results]: []interface {}
RESP [结果]:[]界面{}
RESP[results]: [map[code:%!s(float64=500) errors:[Configuration file 'conf.d/hosts/c2-web-2.conf' already exists.] status:Object could not be created.]]<
RESP [结果]:[map [code:%!s(float64 = 500)错误:[配置文件'conf.d / hosts / c2-web-2.conf'已存在。] status:无法创建对象。 ] <
Now what i want to access it the "code", "errors" and "status" which is in resp["results"] This looks like an array or slice and I've tried indexing it but I get the error at compile time
现在我想要访问它的“代码”,“错误”和“状态”在resp [“结果”]这看起来像一个数组或切片我尝试索引它但我在编译时得到错误
./create_host.go:62: invalid operation: resp["results"][0] (type interface {} does not support indexing)
./create_host.go:62:无效操作:resp [“results”] [0](类型interface {}不支持索引)
I've done a lot of googling, tried unmarshalling the data within resp["results"] etc, but after a few days I have not made much progress.
我已经做了很多谷歌搜索,尝试解组resp [“结果”]等数据,但几天后我没有取得多大进展。
How should I access the map which seems to be a member of an array? The data structure is not guaranteed so I can't create a structure and unmarshall into that.
我应该如何访问似乎是数组成员的地图?数据结构不能保证,所以我无法创建一个结构并解组。
Thanks
2 个解决方案
#1
1
A co-worker provided the code fragement below which made it possible to access the map entries I was looking for.
一位同事提供了下面的代码片段,可以访问我正在寻找的地图条目。
respBody, _ := ioutil.ReadAll(response.Body)
var rsp interface{}
if err := json.Unmarshal(respBody, &rsp); err != nil {
log.Fatal(err)
}
resultMap := rsp.(map[string]interface{})["results"].([]interface{})[0].(map[string]interface{})
fmt.Printf("test: %s<\n", resultMap["errors"] )
test: [Configuration file 'c2-web-2.conf' already exists.]<
test:[配置文件'c2-web-2.conf'已经存在。] <
#2
0
I believe you need to do a type assertion. You have an interface{}
, but you need some sort of slice to index into. Try resp["results"].([]interface{})[0]
? (Sorry, haven't had a chance to test this myself.)
我相信你需要做一个类型断言。你有一个接口{},但你需要某种切片来索引。试试resp [“results”]。([] interface {})[0]? (对不起,我自己没有机会测试过这个。)
#1
1
A co-worker provided the code fragement below which made it possible to access the map entries I was looking for.
一位同事提供了下面的代码片段,可以访问我正在寻找的地图条目。
respBody, _ := ioutil.ReadAll(response.Body)
var rsp interface{}
if err := json.Unmarshal(respBody, &rsp); err != nil {
log.Fatal(err)
}
resultMap := rsp.(map[string]interface{})["results"].([]interface{})[0].(map[string]interface{})
fmt.Printf("test: %s<\n", resultMap["errors"] )
test: [Configuration file 'c2-web-2.conf' already exists.]<
test:[配置文件'c2-web-2.conf'已经存在。] <
#2
0
I believe you need to do a type assertion. You have an interface{}
, but you need some sort of slice to index into. Try resp["results"].([]interface{})[0]
? (Sorry, haven't had a chance to test this myself.)
我相信你需要做一个类型断言。你有一个接口{},但你需要某种切片来索引。试试resp [“results”]。([] interface {})[0]? (对不起,我自己没有机会测试过这个。)