Utilizzando RestSharp in Windows Phone 7
-
30-09-2019 - |
Domanda
Sto cercando di utilizzare RestSharp ( http://restsharp.org/ ) in un Windows Phone 7 progetto, ma sto avendo un problema a quanto pare con la libreria Newtonsoft Json.NET che usi RestSharp. Quando sto cercando di eseguire il mio codice in questo modo:
_restClient.ExecuteAsync<Model.Song>(restRequest, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK) { }
else { }
});
sto ottenendo il seguente errore:
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
viene copiato nella cartella Bin della mia applicazione Windows Phone 7, in modo da sto supponendo che viene distribuito al dispositivo, ma in qualche modo non lo caricherà. Qualcuno ha sperimentato / risolto qualcosa di simile? Grazie.
Come richiesto, un esempio di JSON: [{"type":"Song","id":60097,"title":"A Place Where You Belong","artist":{"type":"Artist","id":17,"nameWithoutThePrefix":"Bullet For My Valentine","useThePrefix":false}}]
e le classi:
[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; }
}
Soluzione
Non c'è bisogno di alcuna degli attributi [DataMember]
, non sono utilizzati da RestSharp.
Dato che il JSON restituito è un array, è necessario deserializzare che ad un array:
client.ExecuteAsync<List<Song>>(request, callback);