Json2.js下载地址 json常用处理
Json字符串
var str = '{"code":10,"msg":"codemsg","data":{"data1":"111","data2":"222","array":["11","22","33","44"]}}';
Json对象
var map = {
code:10,
msg:'codemsg',
data: {
data1: '111',
data2: '222',
array: arr
}
};
以下代码是在引用json2.js的前提下
1.Json字符串转Json对象
var str = '{"code":10,"msg":"codemsg","data":{"data1":"111","data2":"222","array":["11","22","33","44"]}}'; //js 自带eval 处理
//eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。
var obj = eval("(" + str + ")"); //Json2.js 处理
var obj2 = JSON.parse(str);
2.Json 对象转Json字符串
var map = {
code:10,
msg:'codemsg',
data: {
data1: '111',
data2: '222',
array: arr
}
};
var param = JSON.stringify(map);
console.log(param);