Search code examples
c#arraysjsonstringify

Convert two dimensional JSON array to C# array


I have a simple question

How can I desterilize a Json stringified two dimensional array to two dimensional array

I have this JSON string

{"1":" 1","2":" 1, 2, 3, 4","3":" 1","4":" 2","5":" 3","6":" 4, 1, 2, 3"}

I want to convert it to

[1][1]

[2][1,2,3,4]

[3][1,4]

[5][3]

[6][4,1,2,3]

Regards


Solution

  • Your json is not an array. It is a dictionary where some values are like 4, 1, 2, 3

    { 
      "1": " 1",
      "2": " 1, 2, 3, 4",
      "3": " 1",
      "4": " 2",
      "5": " 3",
      "6": " 4, 1, 2, 3"
    }
    

    Using Json.Net

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

    Using JavaScriptSerializer

    var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(json);