1

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!!!

asked Nov 11, 2011 at 14:20

3 Answers 3

1

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);
answered Nov 11, 2011 at 14:25
Sign up to request clarification or add additional context in comments.

2 Comments

How would I get the rawStringofJSON from a .json file?
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.
0

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
answered Nov 11, 2011 at 14:28

Comments

0

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;
answered Nov 11, 2011 at 14:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.