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>

原文地址:https://www.cnblogs.com/newmiracle/p/11865573.html