示例1:数组里面含对象
<script> var params = []; for(var i = 0; i < 3; i++){ var param = []; param.push("one"); param.push("two"); param.push("three"); params.push({"group":i,"param":param}); } var json = JSON.stringify(params);//JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。 alert(json); </script>
组成的json串:
[{"group":0,"param":["one","two","three"]},{"group":1,"param":["one","two","three"]},{"group":2,"param":["one","two","three"]}]
示例2:对象里面含数组
var object = {}; var params = []; for(var i = 0; i < 3; i++){ var obj = {}; obj[i] = "abc"; params.push(obj); } object['name'] = 'jack'; object['age'] = 25; object['data'] = params; var json = JSON.stringify(object); console.log(json);
组成的json串:
{"name":"jack","age":25,"data":[{"0":"abc"},{"1":"abc"},{"2":"abc"}]}
推荐一个在线编辑JavaScript的网站 https://runjs.cn/
效果很不错
转载地址:https://blog.csdn.net/loveLifeLoveCoding/article/details/81094568