2016-03-21 52 views
1

我想反序列化這個JSON,但我不斷收到錯誤。有人可以幫幫我嗎?我在哪裏犯了一個錯誤?如何反序列化與JsonConvert.DeserializeObject這個JSON

JSON: 

{ 
    "totalItems": 63, 
    "items": [ 
    { 
     "id": 100039812, 
     "group": { 
      "code": "DD", 
      "description": "Delivery Documents" 
     }, 
     "type": { 
      "code": "READ", 
      "description": "Logs" 
     }, 
     "reference": "ARLDR", 
     "date": "2015-03-24T00:00:00", 
     "description": "ALogs", 
     "notes": "", 
     "lastUpdateDate": "2015-03-24T14:06:42.063", 
     "location": "BOX A001", 
     "metadata": {} 
    }, 
    { 
     "id": 100039813, 
     "group": { 
      "code": "DD", 
      "description": "Delivery Documents" 
     }, 
     "type": { 
      "code": "BL", 
      "description": "Logbooks" 
     }, 
     "reference": "BALB", 
     "date": "2015-03-24T00:00:00", 
     "description": "Logbooks", 
     "notes": "", 
     "lastUpdateDate": "2015-03-24T14:07:42.44", 
     "location": "BOX A001", 
     "metadata": {} 
     } 
    ] 
} 

public class Documents 
{ 
    public int totalItems { get; set; } 
    public List<doc_items> items { get; set; } 
} 

public class doc_items 
{ 
    public int id { get; set; } 
    public List<group_items> group { get; set; } 
    public List<type_items> type { get; set; } 
    public string reference { get; set; } 
    public string date { get; set; } 
    public string description { get; set; } 
    public string notes { get; set; } 
    public string lastUpdateDate { get; set; } 
    public string location { get; set; } 
    public List<metadata_list> metadata { get; set; } 
} 

public class group_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class type_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class metadata_list 
{ 

} 

然後我把這個:

Documents myDocuments = JsonConvert.DeserializeObject<Documents>(responsetext.ToString()); 

,並收到以下錯誤:

Error: Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type System.Collections.Generic.List`1[AerData.Ranorex.Web.Records.API_Documents+Documents]' because the type requires a JSON array (e.g. [...

+0

將你的json字符串粘貼到一個新的類文件中,使用編輯 - >選擇性粘貼 - >將JSON粘貼爲類,或使用http://jsonutils.com/ - 你的類不太匹配 – Plutonix

回答

1

相反「羣」,「型」和「元」不是數組這樣修改你的類:

public class doc_items 
{ 
    public int id { get; set; } 
    public group_items group { get; set; } 
    public type_items type { get; set; } 
    public string reference { get; set; } 
    public string date { get; set; } 
    public string description { get; set; } 
    public string notes { get; set; } 
    public string lastUpdateDate { get; set; } 
    public string location { get; set; } 
    public metadata_list metadata { get; set; } 
} 
0

Documents類定義的基礎上,一個doc_itemsgroup財產必須是數組。在給定的JSON中,它是一個對象。這同樣適用於在type財產

0

我建議使用一個轉換器是這樣的:http://json2csharp.com/#

試試這個:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var sr = new StreamReader("json.json"); 
     var jsonText = sr.ReadToEnd(); 
     Documents myDocuments = JsonConvert.DeserializeObject<Documents>(jsonText); 
    } 
} 

public class group_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class type_items 
{ 
    public string code { get; set; } 
    public string description { get; set; } 
} 

public class metadata_list 
{ 
} 

public class doc_items 
{ 
    public int id { get; set; } 
    public group_items GroupItems { get; set; } 
    public type_items TypeItems { get; set; } 
    public string reference { get; set; } 
    public string date { get; set; } 
    public string description { get; set; } 
    public string notes { get; set; } 
    public string lastUpdateDate { get; set; } 
    public string location { get; set; } 
    public metadata_list MetadataList { get; set; } 
} 

public class Documents 
{ 
    public int totalItems { get; set; } 
    public List<doc_items> items { get; set; } 
} 

你的數據文件json.json

{ 
    "totalItems": 63, 
    "items": [ 
    { 
     "id": 100039812, 
     "group": { 
     "code": "DD", 
     "description": "Delivery Documents" 
     }, 
     "type": { 
     "code": "READ", 
     "description": "Logs" 
     }, 
     "reference": "ARLDR", 
     "date": "2015-03-24T00:00:00", 
     "description": "ALogs", 
    "notes": "", 
    "lastUpdateDate": "2015-03-24T14:06:42.063", 
    "location": "BOX A001", 
    "metadata": { } 
}, 
{ 
    "id": 100039813, 
    "group": { 
    "code": "DD", 
    "description": "Delivery Documents" 
    }, 
    "type": { 
    "code": "BL", 
    "description": "Logbooks" 
    }, 
    "reference": "BALB", 
    "date": "2015-03-24T00:00:00", 
    "description": "Logbooks", 
    "notes": "", 
    "lastUpdateDate": "2015-03-24T14:07:42.44", 
    "location": "BOX A001", 
    "metadata": { } 
    } 
    ] 
}