Deserialize JSON from API response

匆匆过客 提交于 2019-12-11 13:05:38

问题


I am using an API called "PRTG" that is a server monitoring service. After making a call for all sensors on a device, I get this response in 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 
    }
]
}

I've tried to create a class structure that follows this response. It looks like this:

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; }
}

I'm running this code:

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

It's a base method that's in my WebClient Wrapper that runs the url-query and tries to deserialize it into the class I choose (BasePrtgSensors). The actual call is this:

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

All I'm getting from my object is null and 0 values after the call. I am pretty sure that I've screwed up somewhere in the class-structure, but I'm completely new to serialization and I can't seem to find why the response won't deserialize into my class.

Thanks in advance for any guidance!


回答1:


I've just tested it myself with your code there. I get an exception when using it like so, because of the Dictionary<string, PrtgSensor>.

But, like I said in my comment, if you use a List<PrtgSensor> instead, I get everything into one BasePrtgSensors object.

The reason is, that sensors in json is an array or a list. A dictionary is always a key value pair.



来源:https://stackoverflow.com/questions/29727345/deserialize-json-from-api-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!