0

I'm trying to deserialize a JSON file containing an array of objects into a list of objects using Json.Net. Each object can be composed later from other complex objects like Id

{
"companies": [

        {
            "id": "1",
            "companyName": "A",
            "Id": {
                "patterns": ["\\d{6}"],
                "length": "6",
                "explanation": "Text"
            }
        },

        {
            "id": "2",
            "companyName": "B",
            "Id": {
                "pattern": ["\\d{6}"],
                "explanation": "Text"
            }
        },
 ]
}

I have already tried to implement the following code which is supposed to read the file and deserialize it into a list of objects of type User. But it throws the following error:


Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CodeWars.User]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.


 using (StreamReader file = new StreamReader(@"Path to file"))
{

      string json = file.ReadToEnd();
      List<User> items = JsonConvert.DeserializeObject<List<User>>(json);

}
    public class User
    {
        public int id { get; set; }
        public string companyName { get; set; }
        public Dictionary<string, Id> Id { get; set; }
    }

    public class Id 
    {
        public string pattern { get; set; }
        public string explanation { get; set; }
    }
Alex T
  • 75
  • 5
  • 2
    Your data model does not match your JSON. For one thing you are lacking a root object corresponding to the `{ "companies": [ ] }` container. Go to [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182) and use one of the tools there to auto-generate correct JSON classes. And, to see where you went wrong, try creating an instance of `List` manually in memory and then serializing it, you will see it looks quite different than the JSON you need to deserialize. – dbc Jul 29 '20 at 15:13
  • 1
    I notice the JSON also has an inconsistency-- in the first `Id` object there is a property called `patterns` but in the second it is `pattern`. Is this a typo in the question? Also, why are there two different `id` properties which differ only in case but mean completely different things? – Brian Rogers Jul 29 '20 at 15:26
  • @BrianRogers yes it is a typo in the question. Also, the length property should not be there. The logic is that it will be an array of objects for different companies which will have rules to validate some inputs like Id (which is a fake property just for the question) – Alex T Jul 30 '20 at 06:56
  • Thank you @dbc it did not know that such tools exist. – Alex T Jul 30 '20 at 06:58

0 Answers0