jquery json对象转换
jquery json对象转换
<pre>
//json数组转json串
var arr = [1,2,3, { a : 1 } ];
JSON.stringify( arr );
//json字符串转json数组
var jsonStr = '[1,2,3,{"a":1}]';
JSON.parse( jsonStr );
</pre>
额外小知识:
如果要拼json字符串 以下2种写法 (先拼接json对象 然后再转换成json字符串) 数据和json对象都可以看成 json对象
<pre>
var contact = new Object();
contact["firstname"] = "Jesper";
contact["surname"] = "Aaberg";
console.log(JSON.stringify(contact));
</pre>
<pre>
var sellist = [];
$('.sel').each(function() {
var sel = $(this).val();
sellist.push(sel);
})
var sellist = JSON.stringify(sellist);
</pre>
如果遇到什么不懂的地方直接关注公众号留言(本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。)
作者:newmiracle
出处:https://www.cnblogs.com/newmiracle/