示例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
