Jonas B
Jonas B

Reputation: 29

Convert json string into multidimensional array

This is currently my jSON array:

{"1": [{"11": [[1, 1],[2, 2],[3, 3]],"21": [[1, 1],[2, 2],[3, 3]],"31": [[1, 1],[2, 2],[3, 3]]}],"4": [{"12": [[1, 1],[2, 2],[3, 3]],"22": [[1, 1],[2, 2],[3, 3]],"32": [[1, 1],[2, 2],[3, 3]]}],"6": [{"13": [[1, 1],[2, 2],[3, 3]],"23": [[1, 1],[2, 2],[3, 3]],"33": [[1, 1],[2, 2],[3, 3]]}]}

I want to convert this into Dictionary<int, Dictionary<int, Dictionary<int, int>>>

I have search for a long long time but couldn't find a possible solution. Until now, I had:

Dictionary<int, Dictionary<int, Dictionary<int, int>>> menus = new Dictionary<int, Dictionary<int, Dictionary<int, int>>>();

        JToken entireJson = JToken.Parse(rawDirectory);

        foreach (var item in entireJson.Children())
        {
            var property = item as JProperty;
            var subArray = new Dictionary<int, Dictionary<int, int>>();
            foreach (var subItem in property.Value.Children())
            {
                var subProperty = subItem as JProperty;
                var subSubArray = new Dictionary<int, int>();
                foreach (var subSubItem in subItem)
                {
                    var subSubProperty = subSubItem as JProperty;
                    subSubArray.Add(int.Parse(subSubProperty.Name), int.Parse((String)subSubProperty.Value));
                }
                subArray.Add(int.Parse(subProperty.Name), subSubArray);
            }
            menus.Add(int.Parse(property.Name), subArray);
        }

Edit - Solution

To generate a Dictionary<> as I want, I have to change the jSON array a bit. When you use brackets [] it creates a List<>. When you use curly brackets you can generate a Dictionary<>.

I have resolved my problem by changing the jSON string to:

{"1": {"11": {"1": 1,"2": 2,"3": 3},"21": {"1": 1,"2": 2,"3": 3},"31": {"1": 1,"2": 2,"3": 3}},"4": {"12": {"1": 1,"2": 2,"3": 3},"22": {"1": 1,"2": 2,"3": 3},"32": {"1": 1,"2": 2,"3": 3}},"6": {"13": {"1": 1,"2": 2,"3": 3},"23": {"1": 1,"2": 2,"3": 3},"33": {"1": 1,"2": 2,"3": 3}}}

And used the following code to deserialize:

JsonConvert.DeserializeObject < Dictionary<int, Dictionary<int, Dictionary<int, int>>>>(jSONString)

This is how I generate my three dimensional array:

"{" + string.Join(",", dict.Select(a => String.Format("\"{0}\": {1}", a.Key, String.Join(",", "{" + String.Join(",", a.Value.Select(b => String.Format("\"{0}\": {1}", b.Key, String.Join(",", "{" + String.Join(",", b.Value.Select(c => String.Format("\"{0}\": {1}", c.Key, String.Join(",", c.Value)))) + "}")))) + "}")))) + "}"

Upvotes: 1

Views: 2151

Answers (1)

L.B
L.B

Reputation: 116098

Your json is not Dictionary<int, Dictionary<int, Dictionary<int, int>>> as you are trying to parse.

It is Dictionary<int,List<Dictionary<int,List<List<int>>>>>

Below code works, but It won't be easy to use this structure.


string json = @"{""1"": [{""11"": [[1, 1],[2, 2],[3, 3]],""21"": [[1, 1],[2, 2],[3, 3]],""31"": [[1, 1],[2, 2],[3, 3]]}],""4"": [{""12"": [[1, 1],[2, 2],[3, 3]],""22"": [[1, 1],[2, 2],[3, 3]],""32"": [[1, 1],[2, 2],[3, 3]]}],""6"": [{""13"": [[1, 1],[2, 2],[3, 3]],""23"": [[1, 1],[2, 2],[3, 3]],""33"": [[1, 1],[2, 2],[3, 3]]}]}";

var dict = JsonConvert.DeserializeObject<Dictionary<int,List<Dictionary<int,List<List<int>>>>>>(json);

Upvotes: 1

Related Questions