1

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)))) + "}")))) + "}")))) + "}"
2
  • Could you add in your question what's wrong with your code ? Commented Jul 31, 2014 at 14:35
  • @fxm At your service! Commented Jul 31, 2014 at 18:23

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Thanks! I think I didn't understand the json string. I've changed it a bit so I can use Dictionary<int, Dictionary<int, Dictionary<int, int>>

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.