2016-03-14 101 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")) 
      };