JSON 数组编码

标签 json go

我有一个返回接口(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/

相关文章:

javascript - 如何在javascript中从JSON数组创建值数组

java - Spring RequestBody需要加载嵌套的Object

arrays - Mongo 模式,具有唯一值的字符串数组

google-cloud-datastore - 将数据存储实体放入 Go 中的接口(interface)中

go - 如何增加 klog 级别的冗长程度?

json - jq - 计算匹配选择的项目数

gopls 禁用结构自动填充

go - GO中这个奇怪的函数声明的原因是什么?

html - 使用本地版本时丢失 BootStrap 格式

c# - Newtonsoft JSON.NET 解析为自定义键/值对对象数组