使用map 和array 返回自定义对象的JSON字符串:
function getObjectJSON() { var array = new Array(); for (var i = 0; i < 5; i++) { var map = {}; map[1] = "张三"; map[2] = "李四"; map[3] = "王五"; map[4] = "赵六"; array[i] = map; } return JSON.stringify(array); }
结果:
[{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"},{"1":"张三","2":"李四","3":"王五","4":"赵六"}]
function getObjectJSON2() { var array = new Array(); for (var i = 0; i < 5; i++) { var map = {}; map["name"] = "张三"; map["age"] = "24"; map["height"] = "172cm"; map["address"] = "中国"; array[i] = map; } return JSON.stringify(array); }
结果:
[{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"},{"name":"张三","age":"24","height":"172cm","address":"中国"}]