2016-03-14 102 views
0

在此先感謝您的幫助。使用JSON.net反序列化嵌套對象列表

我有一個JSON文件,其中包含嵌套對象的列表。使用下面的代碼 - 我在DeserializeObject調用中遇到異常。我們使用JSON.net

任何幫助表示讚賞

JSON:

[ 
    { 
     "Email": "[email protected]", 
     "Active": true, 
     "CreatedDate": "2013-01-20T00:00:00Z", 
     "Input": { 
      "User": "Jim", 
      "Admin": "John" 
     }, 
     "Output": { 
      "Version": "12345", 
      "Nylon": "None" 
     } 
    }, 

     { 
     "Email": "[email protected]", 
     "Active": true, 
     "CreatedDate": "2013-01-21T00:00:00Z", 
     "Input": { 
      "User": "Bob", 
      "Admin": "John" 
     }, 
     "Output": { 
      "Version": "12399", 
      "Nylon": "134" 
     } 
    } 
] 

爲了支持我創建了下面的類結構的反序列化。

public class Test002 
    { 
     public class Input 
     { 
      public string User { get; set; } 
      public string Admin { get; set; } 
     } 

     public class Output 
     { 
      public string Version { get; set; } 
      public string Nylon { get; set; } 
     } 

     public class RootObject 
     { 
      public string Email { get; set; } 
      public bool Active { get; set; } 
      public DateTime CreatedDate { get; set; } 
      public Input input { get; set; } 
      public Output output { get; set; } 
     } 

     public class TestCases 
     { 
      public List<RootObject> rootObjects { get; set; } 
     } 

    } 

最後,這裏是對JSON.net的調用JsonConvert.DeserializeObject - 拋出以下異常。

Test002.TestCases tTestCases = JsonConvert.DeserializeObject<Test002.TestCases>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")); 

我想我需要這樣的事情 - 要deseralize對象列表 - 下面的代碼失敗

Test002.TestCases tTestCases = JsonConvert.DeserializeObject<IList<Test002.TestCases>>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")); 

例外:

類型的異常 'Newtonsoft.Json.JsonSerializationException'發生在Newtonsoft.Json.dll中,但未在用戶代碼中處理

附加信息:無法將當前JSON數組反序列化(例如[1,2,3])爲'AP類型ISolution.Test002 + TestCases',因爲該類型需要一個JSON對象(例如{「name」:「value」})來正確反序列化。

要修復此錯誤,請將JSON更改爲JSON對象(例如{「name」:「value」})或將反序列化類型更改爲實現集合接口的數組或類型(例如ICollection,IList)像List,可以從JSON數組中反序列化。 JsonArrayAttribute也可以添加到類型中,以強制它從JSON數組反序列化。

路徑「」,1號線,位置1

+0

你不需要'TestCases'類。它應該反序列化爲一個RootObject對象,其內容爲 – Plutonix

回答

0

這裏的問題是,你嘗試反序列化到IListIList是一個接口,而不是一個具體的類型,所以JSON.NET不知道要創建什麼。你需要告訴它你想要的確切類型:

List<Test002.TestCases> tTestCases = JsonConvert.DeserializeObject<List<Test002.TestCases>>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")); 

你可以將其轉換爲IList這樣的:

IList<Test002.TestCases> tTestCases = JsonConvert.DeserializeObject<List<Test002.TestCases>>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")); 
+0

謝謝Kenneth - 這就是問題所在 – macgowan

1

爲什麼不改的TestCase是一個列表?完美的作品。

public class TestCases : List<RootObject> {} 
0

也許嘗試這樣簡單的東西:

var tTestCases = JsonConvert.DeserializeObject<Test002.RootObject[]>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")); 
0

根據指定的JSON數據,你有RootObjects的一些IEnumerable的。 除了Test002課程外,您的課程構思良好。如果你試圖將json-data反序列化爲List,那麼一切都應該沒問題。嘗試類似

var result = JsonConvert.DeserializeObject<List<RootObject>>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")); 

如果你強烈需要你Test002類的實例,你應該使用

Test002.TestCases result = new TestCases(){ 
      rootObjects = JsonConvert.DeserializeObject<List<RootObject>(File.ReadAllText(@"C:\test\Automation\API\Test002.json")) 
      };