Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a multidimensional javascript array to JSON?

What is the best way of converting a multi-dimensional javascript array to JSON?

like image 660
Kris Jordan Avatar asked Jan 19 '09 20:01

Kris Jordan


People also ask

How to turn a JavaScript array into a JSON?

JS Array to JSON using JSON.stringify() method converts a JavaScript object, array, or value to a JSON string. If you so choose, you can then send that JSON string to a backend server using the Fetch API or another communication library.

Can you convert array to JSON?

You convert the whole array to JSON as one object by calling JSON. stringify() on the array, which results in a single JSON string. To convert back to an array from JSON, you'd call JSON. parse() on the string, leaving you with the original array.

Does JavaScript support multidimensional array?

Javascript has no inbuilt support for multidimensional arrays, however the language is flexible enough that you can emulate this behaviour easily by populating your arrays with separate arrays, creating a multi-level structure.

What is multidimensional array in JavaScript?

A multidimensional array is an array that contains another array. For example, // multidimensional array const data = [[1, 2, 3], [1, 3, 4], [4, 5, 6]];


2 Answers

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 :

https://github.com/douglascrockford/JSON-js

Then, simply include the code and use the JSON.stringify() method on your array.

like image 55
Wookai Avatar answered Sep 30 '22 03:09

Wookai


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> 
like image 37
Mike Foster Avatar answered Sep 30 '22 05:09

Mike Foster