I want to parse a piece of JSON with Newtonsoft Json.NET
JSON:
[{
"type": "switchStatus",
"Data" :[
{
"ID" : "1",
"value" : "2.5"
},
{
"ID" : "2",
"value" : "4.2"
}
],
"Datetime": "2014-12-01",
"customerID": "50"
}]
Classes:
public class Account
{
[JsonProperty("type")]
public string Type { get; set; }
public List<Data> Data { get; set; }
[JsonProperty("Datetime")]
public string DateTime { get; set; }
[JsonProperty("customerID")]
public string CustomerId { get; set; }
}//Account
public class Data
{
[JsonProperty("ID")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
Parsing:
Account account = JsonConvert.DeserializeObject<Account>(message);
Error :
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JSonParser.Account' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.