【发布时间】:2018-01-16 17:03:36
【问题描述】:
我正在尝试在 Go 中验证 JSON 对象。我正在尝试查看 'tags' 属性是否是一个数组。(稍后我还想知道另一个属性是否也是一个对象)。
我已经达到了这一点。如果我打印 reflect.TypeOf(gjson.Get(api_spec, "tags").Value() 我得到:
string // When the field is a string
[]interface {} // When the field is an array
map[string]interface {} // When the field is an object
但是当试图在下面的代码上测试这个时:
if ( gjson.Get(api_spec, "tags").Exists() ) {
if ( reflect.TypeOf(gjson.Get(api_spec, "tags").Value()) != "[]interface {}" ) {
// some code here ...
}
}
我得到以下错误代码:
invalid operation: reflect.TypeOf(gjson.Get(api_spec, "tags").Value()) != "[]interface {}" (mismatched types reflect.Type and string)
提前致谢!
【问题讨论】:
标签: arrays json object go typeof