使用JSon.Net反序列化LiveSDK日历



我是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(。这种方法更好吗?

感谢

您的序列化类似乎与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);

相关内容

  • 没有找到相关文章

最新更新