从Google Places API获取JSON文件 [英] Getting JSON file from Google Places API
问题描述
我尝试使用Places API,最初尝试使用jQuery中的$ .ajax,但在文件的第一个元素上却出现意外的令牌错误.但后来我意识到您无法从Places API获得JSONP.因此,下面是返回值的示例,我一生都无法获得将ID打印到控制台中(对于初学者而言)……有人可以指出正确的方向吗?
I am trying to use the Places API and I was initially trying to use $.ajax from jQuery but I kept getting an unexpected token error on the first element of the file. But I realized then that you can't get JSONP from the Places API. So below is a sample of what it returns, I can't for the life of me get it to print id into console (for starters)...Could someone maybe point me in the right direction?
从地方返回的JSON:
JSON returned from Places:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 33.7862590,
"lng" : -117.8531270
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "5be9a13a28635cf8fd2ef3c62c24d51277a4ba58",
"name" : "Kimmies Coffee Cup",
"rating" : 3.30,
"reference" : "CnRpAAAANKLoOHJ1STFaFavIPjm2bpMDneGTv0AXfixyl-koF7KSK-lQSldk3c01RFJu9Gf62zXnRScte-dkPCBU8KuxCo7AMctfuaIhNkPE8RKENF9HeMlkuEg-Scp97--1VWJvjpNQjc6xRvdXsbf7V8NrSRIQnXjZBKbrSeSg2sTwBosKghoUEFlNDIN5cnVbn5n7aNtwycord-E",
"types" : [ "restaurant", "food", "establishment" ],
"vicinity" : "190 S Glassell St # B, Orange"
}
]
}
这就是我正在尝试的方法,但是显然我不能再使用它了...
And this is what I was trying but obviously I can't use it anymore...
$(document).ready(function () {
$.ajax({
url: 'https://maps.googleapis.com/maps/api/place/search/json',
dataType: 'jsonp',
type: 'GET',
data: {
location: '33.787794,-117.853111',
radius: 100,
name: 'coffee',
key: 'KEY_HERE',
sensor: false,
},
success: function(data) {
console.log(data.results[0].id);
}
});
});
任何建议都会很棒,谢谢!
Any suggestions would be great, thanks!
推荐答案
jQuery有一个parseJSON方法,该方法将从JSON创建一个javascript对象.
jQuery has a parseJSON method which will create a javascript object from JSON.
一旦有了这个对象
obj.results将返回结果数组.
obj.results will return you the results array.
results [0] .id将为您提供第一个结果的ID.
results[0].id will give you the id of the first result.
这篇关于从Google Places API获取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!