Handle json with ajax and use your data separately

1

PHP:

if (@mysql_num_rows($resultados) > 0){
      while ($linha = mysql_fetch_array($sql)) 
          $retorno = array($linha);           
         // print_r(json_encode($retorno));
          echo json_encode($retorno);
}   

How to manipulate, ie break this json with ajax so that you can use your data separately?

The function looks like this: That's how I tried, but it did not work ... Look how I did it:

function marcacao(){       
   $.ajax({
            type: "get",
            url: $server+"/conecta.php",
            data: "acao=marcacao",
            success: function(data) {
                var resultado = JSON.parse(data);
                console.log(resultado);  
            for(var i = 0; i<=data.length; i++){
                $Jlati = resultado.employees[1];
                $Jlng = resultado.employees[2];

                var map = new google.maps.Map(
                        document.getElementById("map"), 
                        { 
                          center : new google.maps.LatLng($Jlati[i], $Jlng[i]), 
                          zoom : 5, 
                          mapTypeId : google.maps.MapTypeId.ROADMAP 
                        }
                );


                var image = 'www/images/ray001.png';

                var marker = new google.maps.Marker(
                    {
                            title : "VOCÊ ESTÁ AQUI: "+$Jlati[i]+", "+$Jlng[i],
                            position : new google.maps.LatLng($Jlati[i], $Jlng[i]),
                            map: map,
                            icon: image
                     });

                marker.setMap(map);  

              }
            }

        });

    }
    marcacao();

But nothing happens ...

    
asked by anonymous 30.11.2015 / 01:17

1 answer

2

Without having the right data of your Json it is difficult to build a code for your specific case, so I'll give you an example of how you can use a Json and then you adapt to your case, okay?

In it we have a Json inside a variable, we parse it into an object, we get an HTML object and put the value of the object generated from Json in HTML.

var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';

obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
<p id="demo">O valor do Json entra aqui.</p>

In the above model the displayed value is Anna Smith and is defined here: obj.employees[1]

Have the code execute you will see the result. This code came from link , in it you can change the code and see the result.

    
30.11.2015 / 02:11