gpt4 book ai didi

json - 如何使用相同的键在嵌套的json对象中构造数组

转载 作者:行者123 更新时间:2023-12-01 22:38:02 25 4
gpt4 key购买 nike

我正在通过Golang中的微服务在Zabbix中创建主机对象。我必须将以下json提供给
Zabbix api创建主机,该主机属于多个组

{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "TEST-HOST",
"interfaces": [
{
"type": 2,
"main": 1,
"useip": 1,
"ip": "0.0.0.0",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "33"
},
{
"groupid": "27"
}
],
"templates": [
{
"templateid": "12156"
}
],
"inventory_mode": 0
},
"auth": "example_token",
"id": 1
}

这段代码返回json对象,其中groups数组为空。

package main

import (
"bytes"
"encoding/json"
"fmt"
)

type Zabbix struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params Params `json:"params"`
Auth string `json:"auth"`
ID int `json:"id"`
}
type Groups struct {
Groupid string `json:"groupid"`
Groupid1 string `json:"groupid"`
}
type Templates struct {
Templateid string `json:"templateid"`
}
type Inventory struct {
Type string `json:"type"`
Tag string `json:"tag"`
TypeFull string `json:"type_full"`
MacaddressA string `json:"macaddress_a"`
MacaddressB string `json:"macaddress_b"`
SerialA string `json:"serialno_a"`
SerialB string `json:"serialno_b"`
}
type Params struct {
Host string `json:"host"`
Interfaces []Interfaces `json:"interfaces"`
Groups []Groups `json:"groups"`
Templates []Templates `json:"templates"`
InventoryMode int `json:"inventory_mode"`
Inventory Inventory `json:"inventory"`
}
type Interfaces struct {
Type int `json:"type"`
Main int `json:"main"`
Useip int `json:"useip"`
IP string `json:"ip"`
DNS string `json:"dns"`
Port string `json:"port"`
}

func main() {

jsonobj := &Zabbix{
Jsonrpc: "2.0",
Method: "host.create",
Params: Params{
Host: "OBU_TEST_123",
Interfaces: []Interfaces{
{
Type: 2,
Main: 1,
Useip: 1,
IP: "10.10.10.10",
DNS: "",
Port: "10050",
},
},
Groups: []Groups{
{
Groupid: "27",
Groupid1: "33",
},
},
Templates: []Templates{
{
Templateid: "12156",
},
},
Inventory: Inventory{
Type: "On Board Unit",
Tag: "test",
TypeFull: "test",
MacaddressA: "test",
MacaddressB: "test",
SerialA: "test",
SerialB: "test",
},
InventoryMode: 0,
},
Auth: "test-token",
ID: 1,
}

byteArray, err := json.Marshal(jsonobj)

if err != nil {
panic(err)
}

fmt.Print(bytes.NewBuffer(byteArray).String())

}

输出:
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "OBU_TEST_123",
"interfaces": [
{
"type": 2,
"main": 1,
"useip": 1,
"ip": "10.10.10.10",
"dns": "",
"port": "10050"
}
],
"groups": [
{}
],
"templates": [
{
"templateid": "12156"
}
],
"inventory_mode": 0,
"inventory": {
"type": "On Board Unit",
"tag": "test",
"type_full": "test",
"macaddress_a": "test",
"macaddress_b": "test",
"serialno_a": "test",
"serialno_b": "test"
}
},
"auth": "test-token",
"id": 1
}

我想念的是什么?有没有更优雅的方法来创建这么大的json对象而不使用结构?

最佳答案

类型Groups struct {
Groupid字符串json:"groupid" Groupid1字符串json:"groupid"}

groupid-两者的值不能相同。如下进行更改,它应该可以工作。
类型Groups struct {
Groupid字符串json:"groupid" Groupid1字符串json:"groupid1"}

关于json - 如何使用相同的键在嵌套的json对象中构造数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59344734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com