为什么这个javascript没有返回餐馆名称?

时间:2015-03-28 05:52:55

标签: javascript json

地址查询

https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurant+in+kolkata&key=API_KEY

RESTAURANT JSON LIST

{
   "html_attributions" : [
      "Listings by \u003ca href=\"http://www.indiacom.com/\"\u003eIndiacom Yellow Pages\u003c/a\u003e"
   ],
   "next_page_token" : "CuQB2wAAALbNaxylA2QNB4pNRsjUxIOYwTSnii1OIwvXqiCPM3736kQZGJ4sEKthpVgvUkskg0ebaLbeE1iNbNKnO7N__X_FpsGprU3o4scQ5aZuUcroSkZVhuWOKvxWHA9IYVXhFmqrVsgG9mjinq-RvANuV6oKzcvnXK09GvdZR0Xp-HINbfoakOVR0TsoOFNCw4UIWrihFSXPJOXeNtTYuImUrPkYKZVt-Y8xMKxr_aOvRR7L0PfcAcXPpSBB2IugIh2K3ESUGMypJD8EuPW1rqqvvYXcMqqHp8iWzq9h1-ytSFl8EhDn2tzr7gU7AkcPORFyLXuiGhSG1bFqUIK2yQb6_fhN-nbym8c17Q",
   "results" : [
      {
         "formatted_address" : "No. 26,Next To Museum,Sudder Street, New Market Area, Jawaharlal Nehru Road, Kolkata, West Bengal 700016, India",
         "geometry" : {
            "location" : {
               "lat" : 22.558687,
               "lng" : 88.350889
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "68f2d647334cdaa6a4063c86347252f5a6ebe4c1",
         "name" : "Zaranj",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "place_id" : "ChIJ24Jtvqd3AjoRFb_Bd8Il8Cw",
         "rating" : 4.2,
         "reference" : "CmRZAAAA9-XMiEF-hYPFZh9kaOBwXQ6R-60XStrKsOEkThALaaeHHmJv6kvEd2JWCh7F9XU9XwyNaNRj6KZQkgGEvhhD8qTIs2fiH-Cng5kmlbIpuQLzQGFgQ8dgnvmL2U1-Uz3REhBb9bJSg-t8S8GDpGUGqlsSGhQnqUVV75IG_AFtVpeV4Xpg2YeTmA",
         "types" : [ "restaurant", "food", "establishment" ]
      },

JS

//Get Restaurant list
function getRestaurants()
{
    var search=$("#search").val();
    var prestring="restaurant+in+";

    var jsonRList="https://maps.googleapis.com/maps/api/place/textsearch/json?query="+prestring+search+"&key=API_KEY";
    alert(jsonRList);//returning the address correctly
    $.getJSON(jsonRList, function (data1) {
        var Rname=data1.results[0].name;
        alert(Rname);//returning nothing

    });
}

为什么这个javascript没有返回餐馆名称?(测试第一个结果[0])

它有什么问题?

alert(Rname); //什么都不返回

1 个答案:

答案 0 :(得分:0)

我认为您可以使用console.log()alert()document.write(),只要信息被反序列化,我使用JSON.parse()进行反序列化,eval()是以前人们用过的东西。 反序列化意味着恢复json中编码的数据,以便Javascript可以将其用作对象。实际上,如果你使用write.document(someJson),你会看到[object object]意味着你已准备好使用所有数据,但你需要先JSON.parse()它。 JSON.stringnify()与您想要做的相反,因为该函数用于序列化数据然后传输它,然后接收器需要使用JSON.parse()对其进行反序列化在Javascript中使用它。 我喜欢使用write.document()来查看信息,但是控制台日志可以让您更轻松地查看对象内部的内容。这里有一些代码示例:

// apiData would be the variable with the API information
var externalData = JSON.parse(apiData);

// will output [object object]     
document.write(externalData);

// will output  “     “
document.write('      ');

// will output the latitude
document.write(externalData.results[0].geometry.location.lat);

// will output in the console, you need to type externalData in the console log 
console.log(externalData);

我希望这会有所帮助。

为了澄清,我使用这个答案回答了另一个论坛中的类似问题How to get geocode latitude and longitude from json google map api in variables by javascript?