Javascript 了解如何使用jQuery解析JSON

Javascript 了解如何使用jQuery解析JSON,javascript,jquery,json,Javascript,Jquery,Json,我很感激有很多关于这个的帖子,也有很多关于谷歌的帖子,但我很难理解如何分解JSON流并访问数据 我有一个简单的JSON响应: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-122.4211908, 37.7564513]}, "properties": { "id": "2950648771574984913", "a

我很感激有很多关于这个的帖子,也有很多关于谷歌的帖子,但我很难理解如何分解JSON流并访问数据

我有一个简单的JSON响应:

{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4211908, 37.7564513]},
"properties": {
"id": "2950648771574984913",
"accuracyInMeters": 80,
"timeStamp": 1309323032,
"reverseGeocode": "San Francisco, CA, USA",
"photoUrl": "https://www.google.com/latitude/apps/badge/api?type=photo&photo=uuRL2jABAAA.9fWeRzNpS-tdX0cqHxxclg.7zdBNW-Rb634EIkOgyO8sw",
"photoWidth": 96,
"photoHeight": 96,
"placardUrl": "https://www.google.com/latitude/apps/badge/api?type=photo_placard&photo=uuRL2jABAAA.9fWeRzNpS-tdX0cqHxxclg.7zdBNW-Rb634EIkOgyO8sw&moving=true&stale=true&lod=1&format=png",
"placardWidth": 56,
"placardHeight": 59
}
}
]
}
我正在尝试访问其中的所有数据,例如:

两个坐标。 反向编码。 等等

我构建了这样一个函数:

    function findTristan(){
            var FindUrl = "/proxy.php";

            var tristanData = $.getJSON(FindUrl,function(json){});

            // this is the part I have failed to get right.
            alert(tristanData.coordinates);

    }

您需要使用
$.parseJSON
函数将JSON转换为js数组:

问候,


Max

您需要使用
$.parseJSON
函数将JSON转换为js数组:

问候,


Max

由于AJAX的异步特性,您只需要在成功回调中操作数据,因为只有在成功回调中才能使用这些数据。
$.getJSON
函数立即返回,并且不返回AJAX请求的结果。因此,应使用代码中保留为空的匿名回调:

function findTristan() {
    var FindUrl = '/proxy.php';
    $.getJSON(FindUrl, function(json) {
        var lat = json.features[0].geometry.coordinates[0];
        var lon = json.features[0].geometry.coordinates[1];
        alert('lat: ' + lat + ', lon: ' + lon);
    });
}

由于AJAX的异步特性,您只需要在成功回调中操作数据,因为只有在成功回调中才能使用这些数据。
$.getJSON
函数立即返回,并且不返回AJAX请求的结果。因此,应使用代码中保留为空的匿名回调:

function findTristan() {
    var FindUrl = '/proxy.php';
    $.getJSON(FindUrl, function(json) {
        var lat = json.features[0].geometry.coordinates[0];
        var lon = json.features[0].geometry.coordinates[1];
        alert('lat: ' + lat + ', lon: ' + lon);
    });
}

这两个函数应该用于
getJSON
complete函数中

json.features[0].geometry.coordinates[0]
json.features[0].geometry.coordinates[1]
发出Ajax请求后,您不应该发出警报。为什么?因为Ajax调用本质上是异步的。这仅仅意味着请求被发送出去,您的代码立即继续执行,而无需等待请求得到响应。这就是为什么在从服务器返回结果之前执行
警报的原因(当然没有任何结果)

而且
getJSON
也不会像您那样返回数据。它将返回完整函数中的数据,您必须自己使用它

function findTristan(){
    var FindUrl = "/proxy.php";

    var tristanCoords = {};

    $.getJSON(FindUrl, function(data){

        tristanCoords = data.features[0].geometry.coordinates;
        alert("x: " + tristanCoords[0] + ", y: " + tristanCoords[1]);

    });
}
忠告
无论何时你必须使用javascript、对象等,使用(Firefox插件)并调试你的代码。您将能够深入查看对象并准确查看其结构。

这两个选项应用于您的
getJSON
完整函数中

json.features[0].geometry.coordinates[0]
json.features[0].geometry.coordinates[1]
发出Ajax请求后,您不应该发出警报。为什么?因为Ajax调用本质上是异步的。这仅仅意味着请求被发送出去,您的代码立即继续执行,而无需等待请求得到响应。这就是为什么在从服务器返回结果之前执行
警报的原因(当然没有任何结果)

而且
getJSON
也不会像您那样返回数据。它将返回完整函数中的数据,您必须自己使用它

function findTristan(){
    var FindUrl = "/proxy.php";

    var tristanCoords = {};

    $.getJSON(FindUrl, function(data){

        tristanCoords = data.features[0].geometry.coordinates;
        alert("x: " + tristanCoords[0] + ", y: " + tristanCoords[1]);

    });
}
忠告
无论何时你必须使用javascript、对象等,使用(Firefox插件)并调试你的代码。您将能够深入查看对象并准确查看其结构。

我认为您不需要这个。如果服务器发送正确的内容类型头(content-type:application/json)和json数据,jQuery会自动解析它。如果服务器发送正确的内容类型头(content-type:application/json)和json数据,jQuery会自动解析它。这是一个非常有用的示例,感谢您花时间澄清。访问json属性部分中的数据(例如reverseGeocode?@Tristan)的语法是什么,您可以尝试以下方法:
json.features[0].properties.reverseGeocode
。这是一个非常有用的示例,感谢您花时间将其弄清楚。访问json属性部分中的数据(例如reverseGeocode)的语法是什么?@Tristan,您可以尝试以下方法:
json.features[0].properties.reverseGeocode