在 Windows Phone 7 中使用 RestSharp [英] Using RestSharp in Windows Phone 7
问题描述
我正在尝试在 Windows Phone 7 中使用 RestSharp (http://restsharp.org/)项目,但我遇到了一个问题,它似乎与 RestSharp 使用的 Newtonsoft Json.NET 库有关.当我尝试像这样执行我的代码时:
I'm trying to use RestSharp (http://restsharp.org/) in a Windows Phone 7 project, but I'm having an issue it seems with the Newtonsoft Json.NET library that RestSharp uses. When I'm trying to execute my code like so:
_restClient.ExecuteAsync<Model.Song>(restRequest, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK) { }
else { }
});
我收到以下错误:
无法从程序集Newtonsoft.Json.Compact,版本=3.5.0.0,Culture=neutral,PublicKeyToken=30AD4FE6B2A6AEED"加载类型Newtonsoft.Json.Linq.JArray".
Newtonsoft.Json.Compact.dll
被复制到我的 Windows Phone 7 应用程序的 Bin 文件夹,所以我假设它被部署到设备,但不知何故它不会加载它.有没有人经历过/解决过类似的事情?谢谢.
Newtonsoft.Json.Compact.dll
is copied to the Bin folder of my Windows Phone 7 application, so I'm assuming it gets deployed to the device, but somehow it won't load it. Has anyone experienced/solved something similar? Thanks.
根据要求,JSON 示例:[{"type":"Song","id":60097,"title":"A Place Where You Belong","artist":{"type":"Artist","id":17,"nameWithoutThePrefix":"Bullet For My Valentine","useThePrefix":false}}]
As requested, an example of the 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 不使用它们.
You don't need any of the [DataMember]
attributes, they're not used by RestSharp.
由于返回的 JSON 是一个数组,您需要将其反序列化为一个数组:
Since the JSON returned is an array, you need to deserialize that to an array:
client.ExecuteAsync<List<Song>>(request, callback);
这篇关于在 Windows Phone 7 中使用 RestSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!