Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)))) + "}")))) + "}")))) + "}"
share|improve this question
    
Could you add in your question what's wrong with your code ? –  fxm 22 hours ago
    
@fxm At your service! –  Jonas B 18 hours ago

1 Answer 1

up vote 0 down vote accepted

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);
share|improve this answer
    
+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>> –  Jonas B 21 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.