首页 > 代码库 > javascript相关,格式转化

javascript相关,格式转化

比如这样的数据:

Object {1018: "3", 40101: "727", 40102: "11032", 40201: "130", 40301: "311", 40401: "798", 40501: "978", 40901: "198", 41003: "82", 41101: "1860", 41102: "1968", 41401: "33", 41404: "86", 41501: "467", 41502: "114", 41505: "158", 41601: "22", 41706: "927", 41801: "9153", 41802: "472", 41805: "1779", 42101: "88", 42103: "6", 42201: "420", 42401: "159", 42603: "28", 42801: "7", 42901: "28731", 42905: "24"}

转化成:

[["1018","3"],["40101","727"],["40102","11032"],["40201","130"],["40301","311"],["40401","798"],["40501","978"],["40901","198"],["41003","82"],["41101","1860"],["41102","1968"],["41401","33"],["41404","86"],["41501","467"],["41502","114"],["41505","158"],["41601","22"],["41706","927"],["41801","9153"],["41802","472"],["41805","1779"],["42101","88"],["42103","6"],["42201","420"],["42401","159"],["42603","28"],["42801","7"],["42901","28731"],["42905","24"]]

程序:

function transform(obj){
var arr = [];
for(var item in obj){
var tmp = [];
tmp.push(item);
tmp.push(obj[item]);
arr.push(tmp);
}
return JSON.stringify(arr);
}
JSON.parse()------>用于从一个字符串中解析出json对象
JSON.stringify()------->用于从对象中解析出json字符串

javascript相关,格式转化