我解析一個JSON字符串變成一個ObservableCollection,但是當我做Json.net拋出這個錯誤:Json.net反序列化錯誤
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MVPTracker.ViewModels.DataModels+League+Position' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
我的DataModel,視圖模型和加載低於:
的DataModel:
public class League
{
public string name { get; set; }
public string code { get; set; }
public string imageUrl { get; set; }
public Position positions = new Position();
public class Position
{
public string name { get; set; }
public string code { get; set; }
public string imageUrl { get; set; }
public string[] statistics { get; set; }
}
}
加載/視圖模型:
private ObservableCollection<DataModels.League> _leagues = new ObservableCollection<DataModels.League>();
public ObservableCollection<DataModels.League> Leagues
{
get { return _leagues; }
set { _leagues = value; NotifyPropertyChanged("Leagues"); }
}
public async void Load()
{
string leaguesJSON = await ServerConnector.LoadOrganizations();
Leagues.Clear();
Leagues = JsonConvert.DeserializeObject<ObservableCollection<DataModels.League>>(leaguesJSON);
}
我試着設置ObservableCollection的IList/ICollection的無濟於事。
編輯:這是我解析JSON:http://pastebin.com/QVnikitV
它完美的作品。非常感謝,不敢相信我忽視了這麼小的事情。 –
沒問題 - 有時只需要第二組眼睛。 –