So ... I'm trying to parse out JSON into an object. It seems to work perfectly for some code, but not for others. I'm not sure what I'm doing wrong. I don't get an error, it simply does not populate.
Source Code:
public AlarmQuery GetAlarms(int startDate, int endDate)
{
string url = string.Format("https://{0}/rest/v1/events?categ_filter=alarm&date_from={1}&date_to={2}&location_ids={3}", webServiceURL, startDate, endDate, "2131012");
var result = getWebResult(url);
var parsedLoc = JObject.Parse(result);
AlarmQuery tmpObj = JsonConvert.DeserializeObject<AlarmQuery>(result);
return tmpObj;
}
Source Class
public class AlarmQuery
{
public class Meta
{
public int count { get; set; }
public int total_count { get; set; }
public double date_from { get; set; }
public double date_to { get; set; }
}
public class ExtraField
{
public string name { get; set; }
public string value { get; set; }
}
public class Attributes
{
public string category { get; set; }
public int entity_id { get; set; }
public string affected_location_ids { get; set; }
public double timestamp { get; set; }
public IList<ExtraField> extra_fields { get; set; }
public string iobject { get; set; }
public IList<object> comments { get; set; }
public string entity { get; set; }
public object channel_id { get; set; }
public int num { get; set; }
public string user { get; set; }
public string msg { get; set; }
public object host_id { get; set; }
public object location_id { get; set; }
public int? device_id { get; set; }
}
public class Datum
{
public Attributes attributes { get; set; }
public string type { get; set; }
public string id { get; set; }
}
public class Jsonapi
{
public string version { get; set; }
}
public class Links
{
public string next { get; set; }
public string self { get; set; }
public string prev { get; set; }
public string last { get; set; }
public string first { get; set; }
}
public class Example
{
public Meta meta { get; set; }
public IList<Datum> data { get; set; }
public Jsonapi jsonapi { get; set; }
public Links links { get; set; }
}
}
Source JSON:
{
"meta":{
"count":100,
"total_count":8848,
"date_from":1643702146.0,
"date_to":1644998146.0
},
"data":[
{
"attributes":{
"category":"Alarm",
"entity_id":2131012,
"affected_location_ids":"2131008",
"timestamp":1644998097.24,
"extra_fields":[
{
"name":"Error",
"value":"Threshold"
},
{
"name":"Location",
"value":"Watchdog (2131008)"
},
{
"name":"Zone",
"value":"System/Lyon/Building N1/LYO19501001"
},
{
"name":"Threshold Alarm Template",
"value":"LYO_Threshold alarm template"
},
{
"name":"Threshold",
"value":"Low Threshold = 0 Bool."
},
{
"name":"Host",
"value":"VA1-PR-SYS-O5.CR.LOCAL"
},
{
"name":"Device",
"value":"LYO19501001 (19501001)"
},
{
"name":"Channel",
"value":"Watchdog (1)"
},
{
"name":"Time on",
"value":"utc:1644997980"
},
{
"name":"Time off",
"value":"utc:1644998040"
},
{
"name":"Comments",
"value":"_"
},
{
"name":"Reason",
"value":"_"
}
],
"iobject":"Low Threshold Alarm: Location value = 0 Bool for System/Lyon/Building N1/LYO19501001/Watchdog",
"comments":[
],
"entity":"threshold_link",
"channel_id":null,
"num":1435342,
"user":"admin (Administration super user)",
"msg":"Alarm turned off: Threshold Alarm: LYO_Threshold alarm template for type Low Threshold value = 0 Bool for Location: System/Lyon/Building N1/LYO19501001/Watchdog.",
"host_id":null,
"location_id":null,
"device_id":null
},
"type":"events",
"id":"1435342"
},
{
"attributes":{
"category":"Alarm",
"entity_id":2131012,
"affected_location_ids":"2131008",
"timestamp":1644998039.86,
"extra_fields":[
{
"name":"Error",
"value":"Threshold"
},
{
"name":"Location",
"value":"Watchdog (2131008)"
},
{
"name":"Zone",
"value":"System/Lyon/Building N1/LYO19501001"
},
{
"name":"Threshold Alarm Template",
"value":"LYO_Threshold alarm template"
},
{
"name":"Threshold",
"value":"Low Threshold = 0 Bool."
},
{
"name":"Host",
"value":"VA1-PR-SYS-O5.CR.LOCAL"
},
{
"name":"Device",
"value":"LYO19501001 (19501001)"
},
{
"name":"Channel",
"value":"Watchdog (1)"
},
{
"name":"Time on",
"value":"utc:1644997980"
},
{
"name":"Comments",
"value":"_"
}
],
"iobject":"Low Threshold Alarm: Location value = 0 Bool for System/Lyon/Building N1/LYO19501001/Watchdog",
"comments":[
],
"entity":"threshold_link",
"channel_id":null,
"num":1435340,
"user":"admin (Administration super user)",
"msg":"Alarm turned on: Threshold Alarm: LYO_Threshold alarm template for type Low Threshold value = 0 Bool for Location: System/Lyon/Building N1/LYO19501001/Watchdog. Alarm message: Watchdog.",
"host_id":null,
"location_id":null,
"device_id":null
},
"type":"events",
"id":"1435340"
}
],
"jsonapi":{
"version":"5.1.5.96"
},
"links":{
"next":"/rest/v1/events?page_number=2&page_size=100",
"self":"/rest/v1/events?page_number=1&page_size=100",
"prev":"/rest/v1/events?page_number=1&page_size=100",
"last":"/rest/v1/events?page_number=89&page_size=100",
"first":"/rest/v1/events?page_number=1&page_size=100"
}
}
What I have tried:
I tried modifying the class, and I tried updating the query. The results are populated and the
JOBject.parse(result)
works. It seems the
DeserializeObject
is the hurdle at the moment.