This article describes the JS implementation of converting a two-dimensional array into a json format string operation. Share it for your reference, as follows:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>json</title> <script src="/jquery/2.0.0/"></script> <script> $(function(){ /******Transfer of two-dimensional array***************/ //Define array var arr=new Array(); arr =[[1,2],[1,2],[1,2],[1,2]]; //Initialize a two-dimensional array var i,j; for(i=0;i<;i++){ var arr_l=new Array();//Get a one-dimensional array in a two-dimensional array arr_1=arr[i]; //Transfer through one-dimensional array for(j=0;j<arr_1.length;j++){ //alert(arr_1[j]); // arr_1: represents the value in a single array (arr_1[j]); } } ("<br>"); var array = [[10, 100, 1000], [1000, 100, 10]]; // encodeArray2D method var jsonStr = encodeArray2D(array); //alert(jsonstr); (jsonStr); ("<br>"); // arrayToJson method var jsonStr2 = arrayToJson(array); (jsonStr2); }); /** * Convert a 2D array to a json string */ function encodeArray2D(obj) { var array = []; for (var i = 0; i < ; i++) { array[i] = '[' + obj[i].join(',') + ']'; } return '[' + (',') + ']'; } /** * Convert a 2D array to a json string */ function arrayToJson(o) { var r = []; if (typeof o == "string") return "\"" + (/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\""; if (typeof o == "object") { if (!) { for (var i in o) (i + ":" + arrayToJson(o[i])); if (!! && !/^\n?function\s*toString\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test()) { ("toString:" + ()); } r = "{" + () + "}"; } else { for (var i = 0; i < ; i++) { (arrayToJson(o[i])); } r = "[" + () + "]"; } return r; } return (); } </script> </head> <body> </body> </html>
useOnline HTML/CSS/JavaScript code running tool:http://tools./code/HtmlJsRunThe test run results are as follows:
12121212
[[10,100,1000],[1000,100,10]]
[[10,100,1000],[1000,100,10]]
PS: Regarding json operation, here are some more practical json online tools for your reference:
OnlineJSONCode verification, inspection, beautification and formatting tools:
http://tools./code/json
JSONOnline formatting tools:
http://tools./code/jsonformat
Online XML/JSONConvert each other tools:
http://tools./code/xmljson
jsonCode online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat
OnlinejsonCompression/escaping tools:
http://tools./code/json_yasuo_trans
For more information about JavaScript, please view the special topic of this site: "Summary of json operation skills in JavaScript》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.