【发布时间】:2018-12-12 19:54:34
【问题描述】:
我正在努力获取以下接口的键和值,这是 JSON 封送Execute 返回的结果的结果,如this example 所示:
[
[
{
"id": 36,
"label": "TestThing",
"properties": {
"schema__testBoolean": [
{
"id": 40,
"value": true
}
],
"schema__testInt": [
{
"id": 39,
"value": 1
}
],
"schema__testNumber": [
{
"id": 38,
"value": 1.0879834
}
],
"schema__testString": [
{
"id": 37,
"value": "foobar"
}
],
"uuid": [
{
"id": 41,
"value": "7f14bf92-341f-408b-be00-5a0a430852ee"
}
]
},
"type": "vertex"
}
]
]
reflect.TypeOf(result) 结果为:[]interface{}。
我用它来循环数组:
s := reflect.ValueOf(result)
for i := 0; i < s.Len(); i++ {
singleVertex := s.Index(i).Elem() // What to do here?
}
但我遇到了以下错误:
reflect.Value.Interface:无法返回未导出的值 字段或方法
【问题讨论】:
-
如果不需要使用
reflection,那么您应该避免使用它并遍历[]interface{}。 -
那么,如果我不知道结构,我会只使用反射吗?
-
@BobvanLuijt:基本上。
标签: go interface type-assertion