将 JSON 数组解析为 Golang 的结构体列表。

huangapple go评论87阅读模式
英文:

Parse json array into list of struct golang

问题

我有一个如下所示的 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 是一个包含每个 clientMetric 对象的 JSON 数组。每个 clientMetric 对象可以具有各种字段。我尝试了以下代码,但是我对如何添加剩余部分感到困惑,因为我来自 Java 背景,而在 Golang 中似乎没有可用的 set。我还不确定如何添加 customerData 对象。

type ClientMetrics struct {
    ClientId    int64
    CatId       int64
    // 这里应该添加其他字段
}

在 Golang 中,将上述 JSON 解组为 ClientMetrics 结构体列表的最佳方法是什么?

英文:

I have a json like below and I am trying to make a struct for below json which can store the data for me when I unmarshall it.

{
"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 is a json array which contains each clientMetric object. Each clientMetric object can have various fields into it. I tried something like below but I am confuse on how to add rest since I am coming from Java background and I don't see there is set available in golang. Also confuse on how to add customerData object too.

type ClientMetrics struct {
ClientId    int64
CatId       int64
}

What is the best way to unmarshall above json into a list of ClientMetrics struct in golang?

答案1

得分: 2

你可以在这里使用json to go:https://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

英文:

You can use json to go here : https://mholt.github.io/json-to-go/

But it will repeat CustomerData struct twice make sure you should remove one of them.

I have created a sample struct for your scenario as follows :

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"`
}

You can run it here in go playground : https://go.dev/play/p/R1M1HfzpEny

答案2

得分: 1

如果你正在使用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选项后使用获得的结构体。

英文:

If you're using VS Code, there are a few extensions that can do this job.
One of them is named Paste JSON as Code.

  1. Install the extension
  2. Copy the JSON and in your clipboard (ctrl+c)
  3. Press Ctrl+Shift+P and select Paste JSON as code
  4. Type name of struct and press enter

If this doesn't work for you, you can always use this site https://mholt.github.io/json-to-go/ but it would be a better practice to use the struct obtained after unselecting Inline type definitions option.

huangapple
  • 本文由 发表于 2021年12月29日 15:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70516591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定