在Windows Phone 7中使用RestSharp

时间:2010-08-21 09:03:03

标签: windows-phone-7 restsharp

我正在尝试在Windows Phone 7项目中使用RestSharp(http://restsharp.org/),但我遇到的问题似乎是RestSharp使用的Newtonsoft Json.NET库。当我试图执行我的代码时:

_restClient.ExecuteAsync<Model.Song>(restRequest, (response) =>
{
    if (response.StatusCode == HttpStatusCode.OK) { }
    else { }
});

我收到以下错误:

Could not load type 'Newtonsoft.Json.Linq.JArray' from assembly 'Newtonsoft.Json.Compact, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30AD4FE6B2A6AEED'.

Newtonsoft.Json.Compact.dll被复制到我的Windows Phone 7应用程序的Bin文件夹中,因此我假设它已部署到设备,但不知何故它不会加载它。有没有人经历/解决类似的事情?感谢。


根据要求,JSON的示例:[{"type":"Song","id":60097,"title":"A Place Where You Belong","artist":{"type":"Artist","id":17,"nameWithoutThePrefix":"Bullet For My Valentine","useThePrefix":false}}]

班级:

[DataContract]
public class Song
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "title")]
    public string Title { get; set; }

    [DataMember(Name = "artist")]
    public Artist Artist { get; set; }
}

[DataContract]
public class Artist
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "nameWithoutThePrefix")]
    public string Name { get; set; }

    [DataMember(Name = "useThePrefix")]
    public bool UsePrefix { get; set; }
}

1 个答案:

答案 0 :(得分:3)

您不需要任何[DataMember]属性,RestSharp不会使用它们。

由于返回的JSON是一个数组,因此需要将其反序列化为数组:

client.ExecuteAsync<List<Song>>(request, callback);