Skip to content

搜索获取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);

标签:

订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论

0
希望看到您的想法,请您发表评论x