【问题标题】:Which data structure i should use for host.create method using Zabbix api? [closed]我应该为使用 Zabbix api 的 host.create 方法使用哪种数据结构? [关闭]
【发布时间】:2019-08-13 15:02:03
【问题描述】:

我想调用host.create 方法(Zabbix API)将主机添加到我的监控系统。问题是我无法格式化数据结构以适应 JSON 请求。 在这里你可以看到它的样子: https://www.zabbix.com/documentation/4.0/manual/api/reference/host/create 问题出在接口部分。看起来它应该是这样的字典数组:Dictionary<string, string>[] hostInterfaces = new Dictionary<string, string>。但是 Newtonsoft.Json 库无法使用这种结构构建 JSON 请求。还有什么可以适应这种情况?

【问题讨论】:

  • 接口应该是List<Dictionary<string, object>>

标签: c# api zabbix


【解决方案1】:

主机接口应该是documentation of Zabbix 中描述的对象数组(至少一个)。

Newtonsoft json 支持serialization of collections:

Product p1 = new Product
{
    Name = "Product 1",
    Price = 99.95m,
    ExpiryDate = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc),
};
Product p2 = new Product
{
    Name = "Product 2",
    Price = 12.50m,
    ExpiryDate = new DateTime(2009, 7, 31, 0, 0, 0, DateTimeKind.Utc),
};

List<Product> products = new List<Product>();
products.Add(p1);
products.Add(p2);

string json = JsonConvert.SerializeObject(products, Formatting.Indented);
//[
//  {
//    "Name": "Product 1",
//    "ExpiryDate": "2000-12-29T00:00:00Z",
//    "Price": 99.95,
//    "Sizes": null
//  },
//  {
//    "Name": "Product 2",
//    "ExpiryDate": "2009-07-31T00:00:00Z",
//    "Price": 12.50,
//    "Sizes": null
//  }
//]

您可以修改此示例来构建您的主机接口对象,例如:

HostInterface int1 = new HostInterface
{
   type = 2,
   main = 1,
   useip = 1,
   ip = 192.168.1.1,
   dns  = "",
   port = 161
};

HostInterface int2 = new HostInterface
{
   type = 1,
   main = 1,
   useip = 1,
   ip = 192.168.1.1,
   dns  = "",
   port = 10050
};


List<HostInterface> HostInterfaces = new List<HostInterface>();
HostInterfaces.Add(int1);
HostInterfaces.Add(int2);

string json = JsonConvert.SerializeObject(HostInterfaces, Formatting.Indented);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多