Google API JSON Return

4

I make a request to a Google API (API Web Service), in which I get a JSON (Matrix) ... My problem is when trying to extract data that are Arrays inside Arrays ... Like field data "Photos", follows a piece of the return JSON.

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.870775,
               "lng" : 151.199025
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
         "id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
         "name" : "Rhythmboat Cruises",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 270,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAAF-LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf-kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvLAXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJhFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
               "width" : 519
            }
         ], 

Data from the first layer I can normally work with a for ();

for(x=0; x < data.results.length; x++){                    
   Vname = data.results[x].name;
}

The problem is in the next layer of information:

Vref_foto = data.results[x].photos[0].photo_reference; ??? ASSIM NÃO FUNCIONA...

Vref_foto = data.results[x].photos.photo_reference; - NEM ASSIM

Vref_foto = data.results[x].photos[0][0]; - NEM ASSIM

Note: I use an AJAX request.

Using ajax:

$.ajax({
    types: "get",
    dataType: "json",
    url: 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='+location+'&radius='+radius+'&name='+name+'&key=AIzaLFTH9jIMt975505wuN_89kccy9r',
    beforeSend: function() {
        $("#coluna_busca_retorno").html("Carregando..."); //Carregando
    },
    error: function (request, status, erro){
        $("#coluna_busca_retorno").html('Ops... Ocorreu algum problema</br>'+status+' : ');
        //Erro
        $("#coluna_busca_retorno").append(request + "-" + status + "-" + erro);
        $("#coluna_busca_retorno").append("sem conexao"); //Erro
    },
    success: function(data){
        alert(data.results[0].name); //FUNCIONA!!!
    }
});
    
asked by anonymous 13.02.2016 / 19:03

1 answer

0

Try this:

$.ajax({
  types: "get",
  dataType: "json",
  url: 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=' + location + '&radius=' + radius + '&name=' + name + '&key=AIzaLFTH9jIMt975505wuN_89kccy9r',
  beforeSend: function() {
    $("#coluna_busca_retorno").html("Carregando..."); //Carregando
  },
  error: function(request, status, erro) {
    $("#coluna_busca_retorno").html('Ops... Ocorreu algum problema</br>' + status + ' : ');
    //Erro
    $("#coluna_busca_retorno").append(request + "-" + status + "-" + erro);
    $("#coluna_busca_retorno").append("sem conexao"); //Erro
  },
  success: function(data) {
    data.results.forEach(function(result) {
      result.photos.forEach(function(photo) {
        photos.push(photo.photo_reference)
      })
    })

    console.log(photos)
  }
});

Note: the console.log(photos) command will log with the array of photos in the console of your browser.

    
24.03.2016 / 16:30