有一個JS數(shù)組,如:
var arr = [["projectname1","projectnumber1"],["projectname2","projectnumber2"],["projectname3","projectnumber3"]];
想將此數(shù)組轉(zhuǎn)換成JSON字符串,如:
var jsonarr = [{"projectname":projectname1,"projectnumber":projectnumber1},{"projectname":projectname2,"projectnumber":projectnumber2},{"projectname":projectname3,"projectnumber":projectnumber3}];
方法如下:
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); }
以上所述是小編給大家介紹的JS中的數(shù)組轉(zhuǎn)變成JSON格式字符串的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答