JS中的數組轉變成JSON格式字符串的方法
更新時間:2017年05月09日 10:40:17 作者:花海_
這篇文章主要介紹了JS中的數組轉變成JSON格式字符串的方法,需要的朋友可以參考下
有一個JS數組,如:
var arr = [["projectname1","projectnumber1"],["projectname2","projectnumber2"],["projectname3","projectnumber3"]];
想將此數組轉換成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中的數組轉變成JSON格式字符串的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!