简体   繁体   中英

Using JSON.net to deserialize a list of nested objects

Thanks in advance for your help.

I have a JSON file that contains a list of nested objects. Using the code below - I get an exception on the call to DeserializeObject. We are using JSON.net

Any help is appreciated

JSON:

[
    {
        "Email": "james@example.com",
        "Active": true,
        "CreatedDate": "2013-01-20T00:00:00Z",
        "Input": {
            "User": "Jim",
            "Admin": "John"
        },
        "Output": {
            "Version": "12345",
            "Nylon": "None"
        }
    },

        {
        "Email": "bob@example.com",
        "Active": true,
        "CreatedDate": "2013-01-21T00:00:00Z",
        "Input": {
            "User": "Bob",
            "Admin": "John"
        },
        "Output": {
            "Version": "12399",
            "Nylon": "134"
        }
    }
]

To support the deserialization I have created the following class structure.

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; }
        }

    }

And finally here is the call to JSON.net JsonConvert.DeserializeObject - throws the following exception.

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

I think I need something like this - to deseralize the list of objects - The code below fails

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

Exception:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON array (eg [1,2,3]) into type 'APISolution.Test002+TestCases' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg 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.

Path '', line 1, position 1.

Why don't change TestCases to be a list? Works perfectly.

public class TestCases : List<RootObject> {}

也许尝试这样简单的事情:

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

The issue here is that you're trying to deserialize into an IList . IList is an interface, not a concrete type so JSON.NET doesn't know what to create. You need to tell it the exact type you want:

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

You could cast that into an IList like this:

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

According to the json-data specified, you got some IEnumerable of RootObjects. Your classes are well-composed, except the Test002 class. Everything should be OK if you try to deserialize json-data as List. Try something like

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

If you strongly need the instance of your Test002 class, you should use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM