I have the following JSON data:
{
"count": 2,
"data" : {
"items" : [
{
"id" : "1",
"letterheadline" : "This is a test",
"message" : "testing.. testing..",
"dateEntered" : "2018-01-01 18:00"
},
{
"id" : "2",
"letterheadline" : "Message two",
"message" : "testing.. testing.. testing..",
"dateEntered" : "2018-02-01 18:00"
},
]
}
}
I am trying to parse it into my own object that uses different values, i.e:
public class Message
{
public string title {get; set;}
public string body {get; set;}
public DateTime entryDate {get; set;}
}
public class Messages
{
public int itemCount {get; set;}
public List<Message> messages {get; set}
}
I am using
Messages messages = new JavaScriptSerializer().Deserialize<Messages>(result);
I have tried to use the following:
[JsonProperty("letterheadline")] (for example)
But I am still getting an error saying that it cannot be converted.
Is this because the JSON data itself is too deep to parse? Therefore would I need to create a new property Data inside my object that contains a list of Messages?
[JsonProperty]is for JSON.NET (NewtonSoft.Json),JavaScriptSerializeris the old, deprecated, built-in JSON library. Use the one you want, but don't mix them[JsonProperty("letterheadline")]is a json.net attribute, it doesn't work forJavaScriptSerializeras explained in JavaScriptSerializer - custom property name.