我是JSon的新手,特别是JSon.Net。我试图在Windows Phone上使用LiveSDK,我遇到了解析JSon响应的问题。我试图阅读日历信息,我无法解析它。下面是我下载Json和我的类定义的代码。我在定义'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);
}
}
}
我的日历课程
[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;}
}
收到JSon格式
{
"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"
},{
...
}
] }
我没有运气使用LiveSDK GetAsync()所以我选择了DownloadAsync()。这种方法更好吗?
由于
答案 0 :(得分:0)
您的序列化类似乎与JSON不匹配。 JSON返回一个对象,其中包含一个名为data
的属性,该属性是一个包含您定义的Calendar
类的元素的数组。
尝试添加一个新类,如:
[JsonObject(MemberSerialization.OptIn)]
public class AppointmentList
{
[JsonProperty(PropertyName="data")]
public List<Calendar> data {get; set;}
}
并反序列化如:
var user = JsonConvert.DeserializeObject<AppointmentList>(json);