【发布时间】:2018-05-24 15:58:09
【问题描述】:
我是 golang 新手,在读取嵌套的 JSON 响应时遇到问题。
var d interface{}
json.NewDecoder(response.Body).Decode(&d)
test :=d["data"].(map[string]interface{})["type"]
response.Body 看起来像这样
{
"links": {
"self": "/domains/test.one"
},
"data": {
"type": "domains",
"id": "test.one",
"attributes": {
"product": " Website",
"package": "Professional",
"created_at": "2016-08-19T11:37:01Z"
}
}
}
我得到的错误是这样的:
invalid operation: d["data"] (type interface {} does not support indexing)
【问题讨论】:
-
这是一个不错的资源gobyexample.com/json
-
@jeff 我正在使用界面,假设我不知道响应的结构。
-
@Jeff 它可以工作,但假设我必须解析嵌套元素,这个解决方案会很复杂。你有其他解决方案吗?
标签: json dictionary go unmarshalling type-assertion