访问Json数据

时间:2016-01-20 04:18:27

标签: json ajax

var myVariable
$.ajax({
        'async': false,
        'global': false,
        dataType: 'json',
        type: "POST",
        url:"connection.php",

        success:function(data){
            myVariable=data;
          }
    });


       $.each(myVariable, function(i,myVariable) {

        console.log(myVariable);
      });

这是我使用ajax获取数据的方式,结果就像

Object {id: "2", lati: "6.03184", lon: "80.2158", description: "A"}
Object {id: "3", lati: "6.03594", lon: "80.2161", description: "B"}
Object {id: "4", lati: "5.97567", lon: "80.4295", description: "C"}
Object {id: "5", lati: "5.98916", lon: "80.3284", description: "D"}
Object {id: "6", lati: "6.1085", lon: "80.2237", description: "E"}
Object {id: "7", lati: "6.14061", lon: "80.0996", description: "F"}
Object {id: "8", lati: "6.95419", lon: "80.7902", description: "G"}

现在我想只获得3条记录 对象{id:“4”,lati:“5.97567”,lon:“80.4295”,描述:“C”} 我怎么能这样做我是这个

的新手

1 个答案:

答案 0 :(得分:0)

如果您希望能够通过密钥访问它,则无法使用" myVariable"作为each()函数中的value参数。

$.each(myVariable, function(i,mv) {
    if(myVariable[i].id === 4){
        var lat = myVariable[i].lati;
        var lon = myVariable[i].lon;
        var des = myVariable[i].description;
        console.log("the lat is "+lat", the lon is "+lon+" the description is "+des);
    }
});