英文:
Type assert a nested interface
问题
我将JSON解析为interface{}
。我无法对其进行类型断言。http://play.golang.org/p/NAe8voYnb8。
package main
import (
"encoding/json"
"log"
)
func main() {
b := []byte(`{"key1":[
{"apple":"A", "banana":"B", "id": "C"},
{"cupcake": "C", "pinto":"D"}
]
}`)
var data interface{}
_ = json.Unmarshal(b, &data)
log.Println(data)
// map[key1:[map[apple:A id:C banana:B] map[cupcake:C pinto:D]]]
ndata, ok := data.(map[string][]map[string]string)
log.Println(ok, ndata)
// false map[]
key_data, ok := ndata["key1"].([]map[string]string)
log.Println(ok, key_data)
// false []
}
有人知道出了什么问题吗?
英文:
I json.Unmarshal
JSON into an interface{}
. I can't type assert it. http://play.golang.org/p/NAe8voYnb8.
package main
import (
"encoding/json"
"log"
)
func main() {
b := []byte(`{"key1":[
{"apple":"A", "banana":"B", "id": "C"},
{"cupcake": "C", "pinto":"D"}
]
}`)
var data interface{}
_ = json.Unmarshal(b, &data)
log.Println(data)
// map[key1:[map[apple:A id:C banana:B] map[cupcake:C pinto:D]]]
ndata, ok := data.(map[string][]map[string]string)
log.Println(ok, ndata)
// false map[]
key_data, ok := ndata["key1"].([]map[string]string)
log.Println(ok, key_data)
// false []
}
Anyone know what's up?
答案1
得分: 5
例如,
package main
import (
"encoding/json"
"log"
)
func somefunc(data interface{}) {
log.Printf("%T\n", data)
// 输出:map[string]interface {}
log.Println(data)
// 正确输出:map[key1:[map[apple:A id:C banana:B] map[cupcake:C pinto:D]]]
ndata, _ := data.(map[string]interface{})
log.Println(ndata)
// 应该输出:map[key1:[map[apple:A banana:B id:C] map[cupcake:C pinto:D]]]
key_data, _ := ndata["key1"]
log.Println(key_data)
// 应该输出:[map[apple:A banana:B id:C] map[cupcake:C pinto:D]]]
}
func main() {
b := []byte(`{"key1":[{"apple":"A", "banana":"B", "id": "C"},{"cupcake": "C", "pinto":"D"}]}`)
var m interface{}
_ = json.Unmarshal(b, &m)
somefunc(m)
}
输出:
2012/10/18 18:21:43 map[string]interface {}
2012/10/18 18:21:43 map[key1:[map[apple:A id:C banana:B] map[cupcake:C pinto:D]]]
2012/10/18 18:21:43 map[key1:[map[banana:B apple:A id:C] map[pinto:D cupcake:C]]]
2012/10/18 18:21:43 [map[banana:B apple:A id:C] map[pinto:D cupcake:C]]
英文:
For example,
package main
import (
"encoding/json"
"log"
)
func somefunc(data interface{}) {
log.Printf("%T\n", data)
// Prints: map[string]interface {}
log.Println(data)
// Correctly prints: map[key1:[map[apple:A id:C banana:B] map[cupcake:C pinto:D]]]
ndata, _ := data.(map[string]interface{})
log.Println(ndata)
// Should print: map[key1:[map[apple:A banana:B id:C] map[cupcake:C pinto:D]]]
key_data, _ := ndata["key1"]
log.Println(key_data)
// Should print: [map[apple:A banana:B id:C] map[cupcake:C pinto:D]]]
}
func main() {
b := []byte(`{"key1":[{"apple":"A", "banana":"B", "id": "C"},{"cupcake": "C", "pinto":"D"}]}`)
var m interface{}
_ = json.Unmarshal(b, &m)
somefunc(m)
}
Output:
2012/10/18 18:21:43 map[string]interface {}
2012/10/18 18:21:43 map[key1:[map[apple:A id:C banana:B] map[cupcake:C pinto:D]]]
2012/10/18 18:21:43 map[key1:[map[banana:B apple:A id:C] map[pinto:D cupcake:C]]]
2012/10/18 18:21:43 [map[banana:B apple:A id:C] map[pinto:D cupcake:C]]
答案2
得分: 1
如果你需要一种简单的处理嵌套JSON数据的方法,我已经编写了一个包来实现这个功能:
https://github.com/opesun/jsonp
这可以减轻在每个级别上都需要输入assert的痛苦。请参阅文档以获取描述。
英文:
If you need an easy way to handle nested JSON data, I have written a package to do that:
https://github.com/opesun/jsonp
This alleviates the pain of having to type assert at every level. See the docs for description.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论