0

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": "[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"
        }
    }
]

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 (e.g. [1,2,3]) into type 'APISolution.Test002+TestCases' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

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

1
  • You dont need TestCases class. It should deserialize to a RootObject object with the stuff inside that Commented Mar 14, 2016 at 19:22

4 Answers 4

1

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

public class TestCases : List<RootObject> {}
Sign up to request clarification or add additional context in comments.

Comments

0

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"));

1 Comment

Thanks Kenneth - that was the problem
0

Perhaps try something as simple as this:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.