【问题标题】:How to struct array in nested json object with the same keys如何使用相同的键在嵌套的 json 对象中构造数组
【发布时间】:2019-12-15 14:13:48
【问题描述】:

我正在通过 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
}

此代码返回groups数组为空的json对象。

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 中,两个字段都有相同的json 标签字段名称,这不好。只需让Groups 有一个Groupid 字段并用值而不是一个值来实例化切片。
  • Groups 是一个对象数组,其中包含一个名为 groupid 的整数字段。这不是你实现的。
  • play.golang.org/p/PDqAOY0lTUI 这是另一种方式。但是,如果您确定模型,则使用接口不是一个好习惯,就像在这种情况下一样。

标签: json go zabbix


【解决方案1】:

类型组结构 { Groupid 字符串json:"groupid" Groupid1 字符串json:"groupid" }

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

【讨论】:

    【解决方案2】:
    type Groups struct {
        Groupid  string `json:"groupid"`
        Groupid1 string `json:"groupid"`
    }
    

    groupid - 两者的值不能相同。如下更改它,它应该可以工作。

    type Groups struct {
        Groupid  string `json:"groupid"`
        Groupid1 string `json:"groupid1"`
    }
    

    【讨论】:

    • 这不是成就。在您的解决方案中,第二个键不同。这意味着 Zabbix api 将拒绝该请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2019-08-15
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多