json数据与字符串的相互转化示例
更新时间:2013年09月18日 15:35:26 作者:
json与字符串之间的转换,在使用中经常会遇到,本文有个不错的示例,大家可以参考下,或许会有所帮助
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'}]
复制代码 代码如下:
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'}]
最新评论