JSON return from Google API

Asked

Viewed 1,086 times

4

I make a request to a Google API (API Web Service), in which returns me a JSON (Matrix)... My problem is when trying to extract data that are Arrays within Arrays... As the data from the "Photos" field, 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
            }
         ], 

First layer data I can work normally 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!!!
    }
});
  • I’m actually trying to get the data in javascript, which does not run with syntax failure, but I also get it in php, I will do the test with var_dump

  • changed the post with more examples

  • data is the return of an AJAX call, and you’re trying to manipulate in PHP? It won’t work like this...

  • is referring to the "for" I posted? It is javascript, sometimes I declare variables with $ in javascript also... The entire post is manipulated in javascript, I put the php tag by mistake, sorry!!! I will change...

  • I’m sorry, I’ve already changed, is that I use both actually, but in question is javascript!!!

  • I still do not have the answer to this question... :(

  • @Rodrigo, your question is hard to answer because you can’t reproduce. That is your problem, how you describe it works normally (https://jsfiddle.net/a9x4m8m0/)... you can clarify the question or reproduce the problem?

  • @Sergio, thank you very much, a friend managed to help me solve the problem, my error was in handling the json in javascript and php, I solved with a "foreach" ['photos'] inside the main "foreach" of json ['Results']. And in javascript it worked this way also with "for". It was right!!! Vlw

  • Okay, so it would be nice to put an answer to the question :)

  • Solved your problem?

Show 5 more comments

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 command console.log(photos) will log the photo array on your browser console.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.