【发布时间】: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