搜索获取JSON的值

在js中接收返回值,比如value之后,把json转换成对象

var json = JSON.parse(value);

一、类似 select age from json where id = 1

var json = [
			{
				"id": "1",
				"num":"123",
				"age": "23"
            },
			{
				"id": "2",
				"num":" 234",
				"age":"20"
			}
		];
function getItem(arr,n,v) {
	for (var i = 0; i < arr.length; i++)
		if (arr[i][n]==v)
			return arr[i];
}

alert(getItem(json,"id","2").age);
alert(getItem(json,"num","123").age);

二、类似 select age from json where id = 1 and num = 123

var json = [
			{
				"id": "1",
				"num":"123",
				"age": "23"
			},
			{
				"id": "2",
				"num":"234",
				"age":"20"
			}
		];
function getItem(arr,obj) {
	arrFor: for (var i = 0; i < arr.length; i++) {
		for (var n in obj)
			if (arr[i][n]!=obj[n])
				continue arrFor;
		return arr[i];
	}
}
alert(getItem(json,{"id":"2","num":"234"}).age);