Home>
There is a js array like:
var arr=[["projectname1", "projectnumber1"], ["projectname2", "projectnumber2"], ["projectname3", "projectnumber3"]];
I want to convert this array into a json string, such as:
var jsonarr=[{"projectname":projectname1, "projectnumber":projectnumber1}, {"projectname":projectname2, "projectnumber":projectnumber2}, {"projectname":projectname3, "projectnumber":projectnumber3}];
Methods as below:
function tojson (arr) {
if (! arr.length) return null;
var i=0;
len=arr.length, array=[];
for (;i<len;i ++) {
array.push ({"projectname":arr [i] [0], "projectnumber":arr [i] [1]});
}
return json.stringify (array);
}
Related articles
- Javascript object json array json string mutual conversion and value method
- JS to json format string as an object or array (front and back)
- Methods to use JSON and restore json to array in PHP language
- Simple implementation of json object and array and conversion into js object
- JS simple loop through the json array method
- How to add json data to js array and the difference between js array and json
- JSON usage of PHP array to JS array, how JS receives PHP array
- 3 ways for JQuery to traverse json array
- Detailed operation of javascript on arrays and json arrays
Trends