arrays - 动态数组json解析

标签 arrays json go

我正在尝试解析 json 以进行 lang struck,但一些对象如何返回空:

Json 对象:

`{
    "names": [
        {
            "David": {
                "id": "100",
                "country": "usa",
                "group": [
                    "A1",
                    "A2"
                ]
            }
        },
        {
            "John": {
                "id": "1",
                "country": "uk",
                "group": [
                    "A1",
                    "A2"
                ]
            }
        }
    ]
}`

GoLang 结构:

type Data struct {
    Names []Names `json:"names"`
}

type Names struct {
    ID      string   `json:"id"`
    Country string   `json:"country"`
    Group   []string `json:"group"`
}

数组包含 2 个元素 David,John 的问题,它以某种方式返回空对象,因为解析器在提取字符串 David,John 时出现问题

最佳答案

结构 Data 的 Names 字段是错误的。它是 map[string]Names 而不是 []Names 的一部分。 所以只需将其更改为:

Names []map[string]Names `json:"names"`

https://play.golang.org/p/IDU0jANRbBn 查看完整的可运行代码.

关于arrays - 动态数组json解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55063022/

相关文章:

go - 无需反射即可将结构转换为元组 slice

go - 如何创建一个不会被复制且不能为空的函数参数

c++ - C++过滤掉数组中的重复值

sql - Postgres SELECT * FROM 表中数组有元素

java - ArrayIndexOutOfBoundsException : 15

json - 为什么 JSON_TABLE() 加入不一致?

javascript - 帮助向对象文字添加新属性

javascript - 如何使用 JavaScript 分隔数组以用于样式目的

c - 在c中使用指针进行字符串操作

http - Golang : how to follow location with cookie