我有一个返回接口(interface){}的函数。如何在不对结构中的字段进行“硬编码”的情况下将其序列化为 JSON 数组。 我正在使用 https://github.com/jmoiron/jsonq返回接口(interface)。
json.Unmarshal(resp.Bytes(), &response)
data := map[string]interface{}{}
dec := json.NewDecoder(strings.NewReader(resp.String()))
dec.Decode(&data)
jq := jsonq.NewQuery(data)
results, err := jq.Array("results")
if err != nil {
log.Fatalln("Unable to get results: ", err)
}
if len(results) == 0 {
return nil
}
return results // this is returning an interface{}
最佳答案
始终可以将 json 字符串解码为 map[string]interface{}
。这就是您需要处理的问题。
关于JSON 数组编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001510/