Using RestSharp in Windows Phone 7

梦想与她 提交于 2019-12-12 15:11:48

问题


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 { }
});

I'm getting the following error:

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 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.


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}}]

And the classes:

[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:


You don't need any of the [DataMember] attributes, they're not used by RestSharp.

Since the JSON returned is an array, you need to deserialize that to an array:

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


来源:https://stackoverflow.com/questions/3537035/using-restsharp-in-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!