英文:
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
。
- 安装这个扩展
- 复制JSON到你的剪贴板中(Ctrl+C)
- 按下Ctrl+Shift+P,选择
Paste JSON as code
- 输入结构体的名称,然后按回车键
如果这对你不起作用,你可以使用这个网站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
.
- Install the extension
- Copy the JSON and in your clipboard (ctrl+c)
- Press Ctrl+Shift+P and select
Paste JSON as code
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论