2012-12-18 2 views
1

내가 ObservableCollection에로 JSON 문자열을 구문 분석하고있어,하지만 난 그것을 할 때 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을 사용하지 않으려 고 시도했습니다.

편집 : C# 코드에서 http://pastebin.com/QVnikitV

답변

2

귀하의 positions 필드 유형 Position의 단일 개체를 나타냅니다 : 여기 구문 분석하고있는 JSON입니다. JSON 객체의 positions 필드는 배열을 나타냅니다.

public Position[] positions { get; set; } 
+0

을 그리고 그것은 완벽하게 작동합니다 :

그래서 C# 코드가 일치하도록 배열로 변경해야합니다. 정말 고마워, 내가 너무 작은 것을 간과 했다니 믿을 수 없어. –

+0

문제 없습니다. 때로는 두 번째 눈이 필요합니다. –