从API响应反序列化JSON

时间:2015-04-19 08:02:55

标签: c# json serialization prtg

我使用的是名为“PRTG”的API,它是一种服务器监控服务。在对设备上的所有传感器进行调用之后,我在Fiddler中得到了这个响应:

{
"prtg-version": "14.2.10.2114",
"treesize": 6,
"sensors": [
    {
        "objid": 6277,
        "probe": "Koneo Kalmar AB",
        "group": "Koneo Kalmar home",
        "device": "bl0615.systempartner.local",
        "sensor": "Disk Space",
        "status": "Up",
        "status_raw": 3,
        "message": "<div class=\"status\">OK<div class=\"moreicon\"></div></div>",
        "message_raw": "OK",
        "lastvalue": "21 %",
        "lastvalue_raw": 21.0000,
        "priority": 4,
        "favorite": "<span class=\"objectisnotfavorite icon-gray ui-icon ui-icon-flag\" id=\"fav-6277\" onclick=\"_Prtg.objectTools.faveObject.call(this,6277,&apos;toggle&apos;);return false;\"></span>",
        "favorite_raw": 0
    },
    {
        "objid": 6279,
        "probe": "Koneo Kalmar AB",
        "group": "Koneo Kalmar home",
        "device": "bl0615.systempartner.local",
        "sensor": "PING",
        "status": "Up",
        "status_raw": 3,
        "message": "<div class=\"status\">OK<div class=\"moreicon\"></div></div>",
        "message_raw": "OK",
        "lastvalue": "0 msec",
        "lastvalue_raw": 0.0000,
        "priority": 5,
        "favorite": "<span class=\"objectisnotfavorite icon-gray ui-icon ui-icon-flag\" id=\"fav-6279\" onclick=\"_Prtg.objectTools.faveObject.call(this,6279,&apos;toggle&apos;);return false;\"></span>",
        "favorite_raw": 0
    },
    {
        "objid": 6280,
        "probe": "Koneo Kalmar AB",
        "group": "Koneo Kalmar home",
        "device": "bl0615.systempartner.local",
        "sensor": "DNS",
        "status": "Up",
         "status_raw": 3,
        "message": "<div class=\"status\">OK: 127.0.0.1<div class=\"moreicon\"></div></div>",
        "message_raw": "OK: 127.0.0.1",
        "lastvalue": "0 msec",
        "lastvalue_raw": 0.0000,
        "priority": 3,
        "favorite": "<span class=\"objectisnotfavorite icon-gray ui-icon ui-icon-flag\" id=\"fav-6280\" onclick=\"_Prtg.objectTools.faveObject.call(this,6280,&apos;toggle&apos;);return false;\"></span>",
        "favorite_raw": 0
    },
    {
        "objid": 6799,
        "probe": "Koneo Kalmar AB",
        "group": "Koneo Kalmar home",
        "device": "bl0615.systempartner.local",
         "sensor": "Disk Free: C:\\ Label:  Serial Number 34fe9d97", "status": "Up", "status_raw": 3,
        "message": "<div class=\"status\">OK<div class=\"moreicon\"></div></div>",
        "message_raw": "OK",
        "lastvalue": "21 %",
        "lastvalue_raw": 21.2458,
        "priority": 3,
        "favorite": "<span class=\"objectisnotfavorite icon-gray ui-icon ui-icon-flag\" id=\"fav-6799\" onclick=\"_Prtg.objectTools.faveObject.call(this,6799,&apos;toggle&apos;);return false;\"></span>",
        "favorite_raw": 0
    },
    {
        "objid": 6800,
        "probe": "Koneo Kalmar AB",
        "group": "Koneo Kalmar home",
        "device": "bl0615.systempartner.local",
         "sensor": "Disk Free: D:\\ Label:DATA  Serial Number 44bc0bc3", "status": "Up",
        "status_raw": 3,
        "message": "<div class=\"status\">OK<div class=\"moreicon\"></div></div>",
        "message_raw": "OK",
        "lastvalue": "49 %",
        "lastvalue_raw": 49.4901,
        "priority": 3,
        "favorite": "<span class=\"objectisnotfavorite icon-gray ui-icon ui-icon-flag\" id=\"fav-6800\" onclick=\"_Prtg.objectTools.faveObject.call(this,6800,&apos;toggle&apos;);return false;\"></span>",
        "favorite_raw": 0
    },
    {
        "objid": 6801,
        "probe": "Koneo Kalmar AB",
        "group": "Koneo Kalmar home",
        "device": "bl0615.systempartner.local",
         "sensor": "SNMP HP Proliant System Health 1", "status": "Warning", "status_raw": 4,
        "message": "<div class=\"status\">Warning in Disk Controller Status: &#39;Degraded&#39;<div class=\"moreicon\"></div></div>",
        "message_raw": "Warning in Disk Controller Status: &apos;Degraded&apos;",
        "lastvalue": "2 #",
        "lastvalue_raw": 2.0000,
        "priority": 3,
        "favorite": "<span class=\"objectisnotfavorite icon-gray ui-icon ui-icon-flag\" id=\"fav-6801\" onclick=\"_Prtg.objectTools.faveObject.call(this,6801,&apos;toggle&apos;);return false;\"></span>",
        "favorite_raw": 0 
    }
]
}

我尝试创建一个遵循此响应的类结构。它看起来像这样:

public class BasePrtgSensors
{
    [JsonProperty("prtg-version")]
    public string PrtgVersion { get; set; }
    [JsonProperty("sensors")]
    public Dictionary<string, PrtgSensor> Sensors { get; set; }
    [JsonProperty("treesize")]
    public string TreeSize { get; set; }
}

public class PrtgSensor
{
    [JsonProperty("objid")]
    public string Id { get; set; }
    [JsonProperty("probe")]
    public string Probe { get; set; }
    [JsonProperty("group")]
    public string Group { get; set; }
    [JsonProperty("device")]
    public string Device { get; set; }
    [JsonProperty("sensor")]
    public string Sensor { get; set; }
    [JsonProperty("status")]
    public string Status { get; set; }
    [JsonProperty("status_raw")]
    public string StatusRaw { get; set; }
    [JsonProperty("message")]
    public string Message { get; set; }
    [JsonProperty("message_raw")]
    public string MessageRaw { get; set; }
    [JsonProperty("lastvalue")]
    public string LastValue { get; set; }
    [JsonProperty("lastvalue_raw")]
    public string LastValueRaw { get; set; }
    [JsonProperty("priority")]
    public string Priority { get; set; }
    [JsonProperty("favorite")]
    public string Favorite { get; set; }
    [JsonProperty("favorite_raw")]
    public string FavoriteRaw { get; set; }
}

我正在运行此代码:

protected T Execute<T>(string urlSegment)
    {
        return JsonConvert.DeserializeObject<T>(Client().DownloadString(_baseUrl + '/' + urlSegment.TrimStart('/')));
    }

这是我的WebClient Wrapper中的一个基本方法,它运行url-query并尝试将其反序列化为我选择的类(BasePrtgSensors)。 实际的电话是:

public BasePrtgSensors GetSensors(string id)
    {
        return Execute<BasePrtgSensors>(_resultType + _contentSensors + _output + _columnsSensors + "&id=" + id + _auth);
    }

我从对象获得的所有内容在调用后为null且值为0。 我很确定我已经搞砸了类结构中的某个地方,但我完全不熟悉序列化,我似乎无法找到为什么响应不会反序列化到我的班级。

提前感谢任何指导!

1 个答案:

答案 0 :(得分:2)

我刚刚用你的代码在那里测试了它。因为Dictionary<string, PrtgSensor>

,我在使用它时会出现异常

但是,正如我在评论中所说,如果您使用List<PrtgSensor>,我会将所有内容都放入一个BasePrtgSensors对象中。

原因是,json中的sensors是数组或列表。字典始终是键值对。