无法从 JSON 获取数据

Unable to get data from JSON

本文关键字:获取 数据 JSON      更新时间:2023-09-26

我现在正在使用此 JSON 链接访问数据,如果我将鼠标悬停在特定标记上,我想访问地名!

$.getJSON('http://anyorigin.com/get?url=https%3A//maps.googleapis.com/maps/api/place/nearbysearch/json%3Flocation%3D-33.8670522%2C151.1957362%26radius%3D5000%26types%3Dfood%26name%3Dharbour%26sensor%3Dfalse%26key%3DAIzaSyAYMqH361IS1S9SA4atjBPCDlLpJ4mr6Sw&callback=?', function (data) {
    var responseData = data.contents;
});
events: {
    mouseover: function (marker, event, context) {
        console.log(responseData.results[this].name)
    }
},

在这里你可以像这样使用循环

      $.each(responseData , function (index, value) {
            console.log(value.name)
      });

假设响应数据的返回类型是有效的,我怀疑使用结果[this]会导致连贯的数据。

您应该检查 responseData.results 包含的内容,以查看要显示的数据(添加"调试器"并检查 responseData.results)。

var responseData;
$.getJSON('http://...', function (data) 
{
    responseData = data.contents;
});
events: 
{
    mouseover: function (marker, event, context) 
    {
        debugger; // remove after inspect responseData and extract the worthy data
        console.log(responseData.results)
    }
},

在代码中,this引用moverover事件。 所以问题是responseData也作为全局变量。

更新:

这是一个简单的方法

var responseData = data.contents;
    var res = responseData.results;
    for (var i = 0; i < res.length; i++) {
        console.log(res[i].name)
    }

JSFiddle