-1

我是 JSon 的新手,尤其是 JSon.Net。我正在尝试在 Windows Phone 上使用 LiveSDK,但在解析 JSon 响应时遇到问题。我正在尝试读取日历信息,但无法解析。下面是我下载 Json 的代码和我的类定义。我在定义“用户”的行出现异常。

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

谢谢

4

1 回答 1

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);
于 2012-09-13T01:09:16.663 回答