I am new to JSon, especially JSon.Net. I am trying to use the LiveSDK on Windows Phone and i am having an issue parsing out the JSon response. I am trying to read calendar information and I can’t get it to parse. Below is my code for downloading the Json and my class definition. I get an exception on the line defining ‘user’.
void getCal_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
{
if (e.Error == null)
{
using (var reader = new StreamReader(e.Result))
{
var json = reader.ReadToEnd();
var user = JsonConvert.DeserializeObject<Calendar>(json);
}
}
}
My Calendar class
[JsonObject(MemberSerialization.OptIn)]
public class Calendar
{
[JsonProperty(PropertyName="name")]
public string Name{get; set;}
[JsonProperty(PropertyName = "id")]
public string Id{get; set;}
[JsonProperty(PropertyName = "description")]
public string Description{get; set;}
[JsonProperty(PropertyName = "created_time")]
public string CreatedTime{get; set;}
[JsonProperty(PropertyName = "updated_time")]
public string UpdatedTime{get; set;}
[JsonProperty(PropertyName = "from")]
public object From{get; set;}
[JsonProperty(PropertyName = "is_default")]
public bool IsDefault{get; set;}
[JsonProperty(PropertyName = "subscription_location")]
public string SubscriptionLocation{get; set;}
[JsonProperty(PropertyName = "permissions")]
public string Permissions{get; set;}
}
The JSon Format received
{
"data": [
{
"id": "calendar.42d4dbc866f94c83849c88c6eb9985bc",
"name": "Birthday calendar",
"description": "If you have birthdays listed for your contacts, they'll appear on this calendar. You can add more birthdays, but you can't add other types of events.",
"created_time": "2011-08-05T19:41:04+0000",
"updated_time": "2011-08-05T19:41:04+0000",
"from": {
"name": null,
"id": null
},
"is_default": false,
"subscription_location": null,
"permissions": "read"
},{
...
}
]
}
I was no having luck using the LiveSDK GetAsync() so i went with DownloadAsync() instead. Is this approach better?
Thanks
Your serialization class doesn’t seem to match the JSON. JSON is returning an object with one property called
datathat is an array containing elements with theCalendarclass you defined.Try adding a new class like:
and deserialize like: