JS解析JSON

解析JSON对象

var people = {
	"name": "zhangsan",
	"age": 18,
	"address": "beijing"
};

var json = eval('(' + people + ')');
console.log(json.name);
console.log(json.age);
console.log(json.address);

解析JSON数组

var arr = [{
			"name": "zhangsan",
			"age": 18,
			"address": "beijing"
		}, {
			"name": "lisi",
			"age": 19,
			"address": "shanghai"
		}, {
			"name": "wangwu",
			"age": 17,
			"address": "hangzhou"
		}];
for (var i in arr) {
    console.log(arr[i]);
    console.log(arr[i].name);
    console.log(arr[i].age);
    console.log(arr[i].address);
}
  • 如果是字符串形式的需要使用eval函数。
var ev = eval('(' + data + ')');
for(var i in ev){
    cosole.log(ev[i]);
}
posted @ 2019-09-02 10:09  _DC  阅读(2440)  评论(0编辑  收藏  举报