我还在学习Go,但是当涉及到JSON响应数组时,我遇到了困难.每当我试图访问"Objects"数组的嵌套元素时,Go都会抛出异常(类型interface{}不支持索引)
出了什么问题?我怎样才能避免将来犯这个错误?
package main
import (
"encoding/json"
"fmt"
)
func main() {
payload := []byte(`{"query": "QEACOR139GID","count": 1,"objects": [{"ITEM_ID": "QEACOR139GID","PROD_CLASS_ID": "BMXCPGRIPS","AVAILABLE": 19}]}`)
var result map[string]interface{}
if err := json.Unmarshal(payload, &result); err != nil {
panic(err)
}
fmt.Println(result["objects"]["ITEM_ID"])
}
http://play.golang.org/p/duW-meEABJ
编辑:固定链接