我正在尝试使用RestSharp(http://restsharp.org/)在Windows Phone 7项目中,但是我遇到了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; }
}
有帮助吗?

解决方案

您不需要任何一个 [DataMember] 属性,restsharp不使用它们。

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

client.ExecuteAsync<List<Song>>(request, callback);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top