What is the best way of converting a multi-dimensional javascript array to JSON?
Most of the popular JavaScript frameworks have JSON utility functions included. For instance, jQuery has a function that directly calls a url and loads the JSON result as an object : http://docs.jquery.com/Getjson
However, you can get an open-source JSON parser and stringifier from the json website :
Then, simply include the code and use the JSON.stringify() method on your array.
Not sure I fully understand your question, but if you are trying to convert the object into a string of JSON then you probably want to look at the native JSON support in all the new browsers. Here's Resig's post on it. For browsers that don't yet support it try the json2.js library. JSON.stringify(obj) will convert your object to a string of JSON.
The "best" way has been provided by the other posters. If you don't need the full encoding features of the referenced libraries, and only need to encode simple arrays, then try this:
<!DOCTYPE html> <html> <head> <title>Simple functions for encoding Javascript arrays into JSON</title> <script type="text/javascript"> window.onload = function() { var a = [ [0, 1, '2', 3], ['0', '1', 2], [], ['mf', 'cb'] ], b = [ 0, '1', '2', 3, 'woohoo!' ]; alert(array2dToJson(a, 'a', '\n')); alert(array1dToJson(b, 'b')); }; function array2dToJson(a, p, nl) { var i, j, s = '{"' + p + '":['; nl = nl || ''; for (i = 0; i < a.length; ++i) { s += nl + array1dToJson(a[i]); if (i < a.length - 1) { s += ','; } } s += nl + ']}'; return s; } function array1dToJson(a, p) { var i, s = '['; for (i = 0; i < a.length; ++i) { if (typeof a[i] == 'string') { s += '"' + a[i] + '"'; } else { // assume number type s += a[i]; } if (i < a.length - 1) { s += ','; } } s += ']'; if (p) { return '{"' + p + '":' + s + '}'; } return s; } </script> </head> <body> </body> </html>
Oops! Just realized I answered an old question. sorry about that
I've modified a bit the code previously provided... because a JSON has this fotmat: [{"object":{"property_1":"value_1","property_2":"value_2"}}]
So, the code would be...
<!DOCTYPE html> <html> <head> <title>Simple functions for encoding Javascript arrays into JSON</title> <script type="text/javascript"> window.onload = function(){ var a = [['property_1','value_1'],['property_2', 'value_2']]; alert("Comienzo..., paso ////"+a+"\\\\\\ a formato JSON"); var jsonSerialized = array2dToJson(a, 'object'); alert(jsonSerialized); }; // Estructura de JSON [{"object":{"property_1":"value_1","property_2":"value_2"}}] function array2dToJson(a, p, nl) { var i, j, s = '[{"' + p + '":{'; nl = nl || ''; for (i = 0; iConvertir un Array a JSON...
</script> </head> <body> </body> </html>
Bye.
I have the same problem, JSON.stringify() do not convert multidimensional array! We sure, need another way
This will convert all combinations of arrays within objects and vice versa including function names:
function isArray(a){var g=a.constructor.toString();
if(g.match(/function Array()/)){return true;}else{return false;}
}
function objtostring(o){var a,k,f,freg=[],txt; if(typeof o!='object'){return false;}
if(isArray(o)){a={'t1':'[','t2':']','isarray':true}
}else {a={'t1':'{','t2':'}','isarray':false}}; txt=a.t1;
for(k in o){
if(!a.isarray)txt+="'"+k+"':";
if(typeof o[k]=='string'){txt+="'"+o[k]+"',";
}else if(typeof o[k]=='number'||typeof o[k]=='boolean'){txt+=o[k]+",";
}else if(typeof o[k]=='function'){f=o[k].toString();freg=f.match(/^function\s+(\w+)\s*\(/);
if(freg){txt+=freg[1]+",";}else{txt+=f+",";};
}else if(typeof o[k]=='object'){txt+=objtostring(o[k])+",";
}
}return txt.substr(0,txt.length-1)+a.t2;
}