I'm trying to use an API to get reports but I'm having trouble getting the JSON results into usable information using JSON.NET. I've looked at quite a few examples but the correct class format for the classes is eluding me.
JSON Data:
{
"status": "OK",
"data": {
"event": {
"Time": "2015-09-01 17:31:47",
"Username": "user1",
"IP_Address": "1.2.3.4",
"Action": "action1",
"Data": "somedata"
},
"event": {
"Time": "2015-09-01 17:30:30",
"Username": "user2",
"IP_Address": "1.2.3.5",
"Action": "action2",
"Data": "data"
}
}
}
Where I'm at now:
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
public class aRoot
{
[JsonProperty("status")]
public string status { get; set; }
[JsonProperty("data")]
public aData data { get; set; }
}
public class aData
{
[JsonProperty("event")]
public List<aEvents> events { get; set; }
}
public class aEvents
{
[JsonProperty("Time")]
public DateTime time { get; set; }
[JsonProperty("UserName")]
public string user { get; set; }
[JsonProperty("IP_Address")]
public string ip { get; set; }
[JsonProperty("Action")]
public string action { get; set; }
[JsonProperty("Data")]
public string data { get; set; }
}
aRoot objReporting = JsonConvert.DeserializeObject<aRoot>(result);
Anyone have any ideas?
Edit: Error Output:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[aEvents]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data.event.Time', line 1, position 39.