Search code examples
c#jsonserializationjson.netjson-deserialization

How to deserialize from my JSON array to an array of dictionaries whose values are my "Resource" objects in C# .NET using Newtonsoft?


I'm getting the following error when trying to deserliaze my JSON into a Dictionary<string, Resources>[]:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.Dictionary`2[System.String,MyProject.Resource][]' 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.

My Program.Main():

    public class Program
            {
                public static void Main(string[] Args)
                {
                    var options = new Options()
                    {
                        Option1 = "foo",
                        Option2 = "bar",
                        Resources = JsonConvert.DeserializeObject<Dictionary<string, Resource>[]>(resources.json)
                    };
                }
            }

My resources.json:

{
    "global": [

        {
            "title": "global1",
            "url": "~/global1"
        },
        {
            "title": "global2",
            "url": "~/global2"
        }
    ],
    "ACC": [
        {
            "title": "acc1",
            "url": "~/acc1/"
        },
        {
            "title": "acc2",
            "url": "~/acc2/"
        }
    ],
    "ECC": [
        {
            "title": "ecc1",
            "url": "~/ecc1/"
        },
        {
            "title": "ecc2",
            "url": "~/ecc2/"
        }
    ],
    "ECB": [
        {
            "title": "ecb1",
            "url": "~/ecb1"
        }
    ]

}

My Resource class:

public class Resource
    {
        public List<string> titles { get; set; }

        public List<string> urls { get; set; }
    }

My Options class:

public class Options
    {
        public string Option1 { get; set; }
        public string Option2 { get; set; }
        public Dictionary<string, Resource>[] Resources { get; set; }
    }

Solution

  • Your JSON does not represent an array of dictionaries (Dictionary<string, Resource>[]), it represents a dictionary of arrays (Dictionary<string, Resource[]>). That is why you are getting the error.

    Also, your Resource class needs to be defined like this to match the JSON:

    public class Resource
    {
        public string Title { get; set; }
        public string Url { get; set; }
    }
    

    You can then deserialize like this:

     Resources = JsonConvert.DeserializeObject<Dictionary<string, Resource[]>>(resources.json)
    

    See fiddle here: https://dotnetfiddle.net/8s2eQd