当前位置:Gxlcms > JavaScript > json数据与字符串的相互转化示例

json数据与字符串的相互转化示例

时间:2021-07-01 10:21:17 帮助过:4人阅读

json转成string[需要引用json2.js文件]:
代码如下:

var arr=[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}];
var str=JSON.stringify(arr);

结果:"[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}]"

string转化为json:
代码如下:

var str="[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}]";
var json=$.parseJSON(str);

结果:[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}]

人气教程排行