Using RestSharp in Windows Phone 7

2019-07-07 06:19发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-07-07 06:51

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);
查看更多
登录 后发表回答