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);
}
  • Previous The solution of Apache connection to MySQL prompts that the connection has been reset in Win7 environment
  • Next Java equals () method rewrite implementation code
  • Trends