Я звоню в Google Places API, который возвращает ответ JSON, подобный этому, из этого ответа я хотел бы извлечь все объекты имен и сохранить их в массиве:
{
"html_attributions" : [],
"next_page_token" : "CpQCAgEAAFxg8o-eU7_uKn7Yqjana-",
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.867217,
"lng" : 151.195939
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
"id" : "7eaf747a3f6dc078868cd65efc8d3bc62fff77d7",
"name" : "Biaggio Cafe - Pyrmont",
"opening_hours" : {
"open_now" : true
},
"photos" : [
{
"height" : 600,
"html_attributions" : [],
"photo_reference" : "CnRnAAAAmWmj0BqA0Jorm1_vjAvx1n6c7ZNBxyY-U9x99-oNyIBE",
"width" : 900
}
],
"place_id" : "ChIJIfBAsjeuEmsRdgu9Pl1Ps48",
"price_level" : 1,
"types" : [ "cafe", "bar", "restaurant", "food", "establishment" ],
"vicinity" : "48 Pirrama Rd, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.866786,
"lng" : 151.195633
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3ef986cd56bb3408bc1cf394f3dad9657c1d30f6",
"name" : "Doltone House",
"photos" : [
{
"height" : 1260,
"html_attributions" : [ "From a Google User" ],
"photo_reference" : "CnRwAAAAeM-ag",
"width" : 1890
}
],
"place_id" : "ChIJ5xQ7szeuEmsRs6Kj7YFZE9k",
"reference" : "CnRvAAAA22k1PAGyDxAgHZk6ErHh_h_mLUK_8XNFLvixPJHXRbCzg-",
"types" : [ "food", "establishment" ],
"vicinity" : "48 Pirrama Rd, Pyrmont"
},
{
"aspects" : [
{
"rating" : 23,
"type" : "overall"
}
],
...
],
"status" : "OK"
}
В моем код, я хотел бы хранить все экземпляры имени в массиве. Я не совсем уверен, как это сделать, но то, что я делаю сейчас, определенно не работает, массив остается пустым после регистрации в консоли.
var place_search = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" + encodeURIComponent(agent.parameters.cuisine) + "restaurant" + "®ion=sg&location=" + lat + "," + long + "&radius=" + user_proximity + "&opennow" + "&key=" + api_key;
return axios.get(place_search)
.then(response => {
const { place_id, name, formatted_address } = response.data.results;
placeArray.push({
name: name,
place_id: place_id
});
)}