This project has moved and is read-only. For the latest updates, please go here.

deserializing multidimensional string array from Json

Oct 26, 2009 at 2:19 PM


I have the following JSon string which i am unable to change the format :

string jsonStr = "{\"numRecords\":\"2\",\"data\":[[\"apple\", \"orange\"],[\"banana\", \"watermelon\"]]}";

I manage to obtain values for numRecords with the following code :               

                JObject json = JObject.Parse(jsonStr);
                string abc = (string)json["numRecords"]; // correct value is stored in abc

But i have no clue how to assign a multidimensional string array in C# to hold the value for "data" and which object in framework can be used to extract out the values from "data". I tried the following :

 JToken token = json["data"];

which can extract out the multidimensional string array into JToken but no idea how to get the data  to a valid C# type.

Does anyone have any suggestion to offer ?






Oct 26, 2009 at 11:15 PM
Edited Oct 26, 2009 at 11:15 PM

string blah = (string)json["data"][0][0];

Oct 27, 2009 at 2:15 AM
Edited Oct 27, 2009 at 2:20 AM
JamesNK wrote:

string blah = (string)json["data"][0][0];

Thanks James,

If i need to cycle through the array, I used the following code :

JToken token = json["data"];

StringBuilder strbuild =  new StringBuilder();

int countRow=0;

int countCol=0;

foreach (JToken tokrow in token)
                    foreach (JToken tokcol in tokrow)
                        string blah = (string)tokcol[countCol];



I am unable to get the value out. Is there something wrong with the above code ?


Oct 27, 2009 at 3:16 AM

Hi James,

Thanks for the reply, i managed to solved the above problem with the following code:

for (int row = 0; row < json["data"].Count(); row++)
                    for (int col = 0; col < json["data"][row].Count(); col++)
                        arrayValue = (string)json["data"][row][col];