2013-10-05 110 views
3

我使用Json.Net到DeserializeObject JSON數據爲對象/集合(名單)當前JSON對象JSON的JSON陣列無法反序列化

我不知道我在做什麼錯了,我已經試過這:

List<LanguageObject> lang = JsonConvert.DeserializeObject<List<LanguageObject>>(jsonData); 

,並試圖此:

LanguageObject.Results lang = JsonConvert.DeserializeObject<LanguageObject.Results>(jsonData); 

我得到這個錯誤:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LanguageObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 
Path 'meta', line 1, position 8. 

這裏我JSON:

{"meta": 
{ 
    "status":200, 
    "message":[], 
    "resultSet": 
      {"id":"05"}, 
    "pagination":  
    { 
     "count":2, 
     "pageNum":1, 
     "pageSize":2, 
     "totalPages":1, 
     "sort":"ordinal", 
     "order":"Asc", 
     "currentUri":null, 
     "nextUri":null 
     } 
    }, 
    "results": 
    { 
     "id":0, 
     "name": 
     "Language", 
     "DName":null, 
     "qvalue":"language", 
     "language":null, 
     "mUsageCount":null, 
     "Ordinal":0, 
     "items": 
     [ 
      { 
      "id":0, 
      "name":"English", 
      "DName":"English", 
      "qvalue":null, 
      "language":null, 
      "mUsageCount":null, 
      "Ordinal":0, 
      "items":[] 
      }, 
      { 
      "id":0, 
       "name":"Spanish", 
       "DName":"Spanish;", 
       "qvalue":null, 
       "language":null, 
       "mUsageCount":null, 
       "Ordinal":0," 
       items":[] 
      } 
     ] 
    }} 

LanguageObject.cs

Public class LanguageObject 
    { 
    public class ResultSet 
    { 
     public string id { get; set; } 
    } 

    public class Pagination 
    { 
     public int count { get; set; } 
     public int pageNum { get; set; } 
     public int pageSize { get; set; } 
     public int totalPages { get; set; } 
     public string sort { get; set; } 
     public string order { get; set; } 
     public object currentUri { get; set; } 
     public object nextUri { get; set; } 
    } 

    public class Meta 
    { 
     public int status { get; set; } 
     public List<object> message { get; set; } 
     public ResultSet resultSet { get; set; } 
     public Pagination pagination { get; set; } 
    } 

    public class Item 
    { 
     public int id { get; set; } 
     public string name { get; set; } 
     public string DName { get; set; } 
     public object qvalue { get; set; } 
     public object language { get; set; } 
     public object mUsageCount { get; set; } 
     public int Ordinal { get; set; } 
     public List<object> items { get; set; } 
     public List<object> __invalid_name__ 
         items { get; set; } 
    } 

    public class Results 
    { 
     public int id { get; set; } 
     public string name { get; set; } 
     public object DName { get; set; } 
     public string qvalue { get; set; } 
     public object language { get; set; } 
     public object mUsageCount { get; set; } 
     public int Ordinal { get; set; } 
     public List<Item> items { get; set; } 
    } 

    public class RootObject 
    { 
     public Meta meta { get; set; } 
     public Results results { get; set; } 
    } 

} 

回答

5

我沒有看到在你的問題中定義的LanguageObject類。但是,我確實看到了RootObject類。從您發佈的東西,你需要反序列化這樣的:

RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonData); 

然後你可以從RootObject得到Results

Results lang = root.Results; 
+0

LanguageObject.cs是我的課我已經更新。但是我的問題,我會給它一個鏡頭併發布我的結果。 –