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.

I have a JSON file that I would like to create an multi-dimensional array from. This information is stored in a JSON file.

Here is the JSON data.

{
  "table": {
    "columnNames": ["column1", "column2", "column3", "column4"],
    "columnTypes": ["String", "String", "String", "String"],
    "rows": [
      ["data00", "data01", "data02", "data03"],
      ["data10", "data11", "data12", "data13"],
      ["data20", "data21", "data22", "data23"],
      ["data30", "data31", "data32", "data33"]
     ]
   }
}

I need to create an array of arrays from the objects in the "rows" section.

Any help would be appreciated!

Thanks!!!

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Once you parse the JSON, the "table.rows" property will already be a multi-dimensional array (2 dimensions, to be specific). All you have to do is access it:

var array2D = parsed.table.rows;

As to parsing, you should probably use something like Crockford's parser:

var parsed = JSON.parse(rawStringOfJSON);
share|improve this answer
    
How would I get the rawStringofJSON from a .json file? –  gberg927 Nov 11 '11 at 14:48
    
Well, that depends on your server application. Generally you'd fetch it with some sort of AJAX interaction, but it could be included in a page in other ways. That probably should be a separate question, as it really doesn't have much to do with the parsing aspect of JSON. –  Pointy Nov 11 '11 at 14:50
add comment

You first need to convert the string to JS object.

Use json2.js from http://www.json.org/js.html

Please keep in mind that you need to add json2.js if you consider to support old browsers. Newer browser has built-in support for JSON

And deserialize the string to object

var myObject = JSON.parse(myJSONtext);

And now you can access

myObject.table.rows[1][2]; // yields data12
share|improve this answer
add comment

The rows section already contains an array of arrays, so just use:

var result = JSON.parse('{
  "table": {
  "columnNames": ["column1", "column2", "column3", "column4"],
  "columnTypes": ["String", "String", "String", "String"],
  "rows": [
    ["data00", "data01", "data02", "data03"],
    ["data10", "data11", "data12", "data13"],
    ["data20", "data21", "data22", "data23"],
    ["data30", "data31", "data32", "data33"]
   ]
  }
}');

var rows = result.table.rows;
share|improve this answer
add comment

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.