1 var json = {"data":[]}; 2 for(var i = 0; i < 4; ++i){ 3 var newjson = {}; 4 newjson["name"] = "白菜"+i; 5 newjson["age"] = 100+i; 6 newjson["type"] = "蔬菜"; 7 json.data.push(newjson); 8 } 9 console.log(json);
在chrome浏览器打印
字符串转换json和数组的访问
var str = '{"data":[{"leg":"6条腿", "name":"小昆虫"},{"leg":"4条腿", "name":"小白菜"},{"leg":"8条腿", "name":"小章鱼"}]}'; var json1 = JSON.parse(str); for(var i = 0; i < json1.data.length; ++i){ console.log(json1.data[i].name+" "+json1.data[i].leg); }
在chrome浏览器打印