【问题标题】:Parse json array into list of struct golang将 json 数组解析为 struct golang 列表
【发布时间】:2022-01-27 15:57:02
【问题描述】:

我有一个像下面这样的 json,我正在尝试为下面的 json 创建一个结构,它可以在我解组时为我存储数据。

{
  "clientMetrics": [
    {
      "clientId": 951231,
      "customerData": {
        "Process": [
          "ABC"
        ],
        "Mat": [
          "KKK"
        ]
      },
      "legCustomer": [
        8773
      ]
    },
    {
      "clientId": 1234,
      "legCustomer": [
        8789
      ]
    },
    {
      "clientId": 3435,
      "otherIds": [
        4,
        32,
        19
      ],
      "legCustomer": [
        10005
      ]
    },
    {
      "clientId": 9981,
      "catId": 8,
      "legCustomer": [
        13769
      ]
    },
    {
      "clientId": 12124,
      "otherIds": [
        33,
        29
      ],
      "legCustomer": [
        12815
      ]
    },
    {
      "clientId": 8712,
      "customerData": {
        "Process": [
          "College"
        ]
      },
      "legCustomer": [
        951
      ]
    },
    {
      "clientId": 23214,
      "legCustomer": [
        12724,
        12727
      ]
    },
    {
      "clientId": 119812,
      "catId": 8,
      "legCustomer": [
        14519
      ]
    },
    {
      "clientId": 22315,
      "otherIds": [
        32
      ],
      "legCustomer": [
        12725,
        13993
      ]
    },
    {
      "clientId": 765121,
      "catId": 8,
      "legCustomer": [
        14523
      ]
    }
  ]
}

clientMetrics 是一个 json 数组,其中包含每个 clientMetric 对象。每个clientMetric 对象都可以包含各种字段。我尝试了类似下面的方法,但我对如何添加休息感到困惑,因为我来自 Java 背景,而且我没有看到 golang 中有可用的设置。也对如何添加customerData 对象感到困惑。

type ClientMetrics struct {
    ClientId    int64
    CatId       int64

  

}

将 json 之上的unmarshall 转换为 golang 中的 ClientMetrics 结构列表的最佳方法是什么?

【问题讨论】:

    标签: json go struct unmarshalling


    【解决方案1】:

    您可以在这里使用json to gohttps://mholt.github.io/json-to-go/

    但它会重复 CustomerData 结构两次确保你应该删除其中一个。

    我为您的场景创建了一个示例结构,如下所示:

    type AutoGenerated struct {
            ClientMetrics []struct {
                ClientID     int `json:"clientId"`
                CustomerData struct {
                    Process []string `json:"Process"`
                    Mat     []string `json:"Mat"`
                } `json:"customerData,omitempty"`
                LegCustomer []int `json:"legCustomer"`
                OtherIds    []int `json:"otherIds,omitempty"`
                CatID       int   `json:"catId,omitempty"`
            } `json:"clientMetrics"`
        }
    

    您可以在 go playground 中运行它:https://go.dev/play/p/R1M1HfzpEny

    【讨论】:

    • 如何强制所有字符串和整数数组的唯一性?我的意思是,由于 golang 中没有设置数据类型,所以我可以在这里做什么?
    • 兄弟你还在吗?
    【解决方案2】:

    如果您使用的是 VS Code,有一些扩展可以完成这项工作。 其中之一被命名为Paste JSON as Code

    1. 安装扩展
    2. 复制 JSON 并在剪贴板中 (ctrl+c)
    3. 按 Ctrl+Shift+P 并选择Paste JSON as code
    4. 输入结构名称并回车

    如果这对您不起作用,您可以随时使用此站点 https://mholt.github.io/json-to-go/,但最好使用取消选择 Inline type definitions 选项后获得的结构。

    【讨论】:

    • 如何强制所有字符串和整数数组的唯一性?我的意思是,由于 golang 中没有设置数据类型,所以我可以在这里做什么? ——
    • 我认为你在这里寻找的是一个 ENUM。虽然,golang 没有开箱即用的支持,但您可以通过使用用户定义的类型来实现相同的功能。 go.dev/play/p/a1wMcpnha4q 为了在 API 请求中强制执行,您可以在尝试将 JSON 绑定到结构的处理程序/资源层中添加自定义检查
    • 啊..你认为你可以提供一个例子让我更好地理解。到目前为止,我有点困惑,我无法弄清楚如何在这里使用 enum 来实现这一点?
    • 你在帮我吗?
    猜你喜欢
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2016-11-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多