JSON string from

1

Dear colleagues:

I am working with a json string with the following format:

[{"id":"1","nombrelonlat":"A;28.00;-15.00"},
 {"id":"","nombrelonlat":";28.20;-15.50"},
 {"id":"2", "nombrelonlat":"b;28.01;-15.01"#"id":"3", "nombre":"c;28.05;-15.05"}]

from latitude longitude I write it in gmap map, all ok except I do not know how to start recursively.

I have the following code

$.ajax({
                        type: "GET",
                        url: 'http://www.grancanariamodacalida.es/json_tiendas.php',
                        async: false,
                        dataType: "json",
                        success: function (data) {
                            //alert(data);
                            $.each(data, function (key, registro) {

                                var id = "";
                                var nombre = "";
                                var lon ="";
                                var lat ="";
                              });
                           $.each(registro, function (campo, valor) {

                                    //alert(campo + ": " + valor);
                                    if (campo == "id") {
                                        id = valor;
                                    }
                                   if (campo == "nombre") {
                                        var punfisdata = valor;
                                        //alert (punfisdata);
                                        if (punfisdata = ""){
                                            nombre= valor.split(";")[0];
                                            //alert(cont1.valueOf());
                                            lon = valor.split(";")[1];
                                            //alert(cont2);
                                            lat = valor.split(";")[2];
                                            //alert(cont3);
                                        }
                             });

But do not shoot me. As you can see some data come empty, others come with a record, others with two or more separated by #.

Any ideas on how to separate them correctly?

S @ | u2.

    
asked by akenateb 26.12.2016 в 15:10
source

1 answer

0

If I understand what you need, replace the success code with this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
var data = [{"id":"1","nombrelonlat":"A;28.00;-15.00"}, {"id":"","nombrelonlat":";28.20;-15.50"},{"id":"2","nombrelonlat":"b;28.01;-15.01"},{"id":"3","nombre":"c;28.05;-15.05"}];                             
 
$.each(data, function(index, item){ 
     var id = item.id;
     var arreglo = item.nombrelonlat.split(";");
     var nombre = arreglo[0];
     var lon = arreglo[1];
     var lat = arreglo[2];
  
     alert("dato" + id + "=" + nombre + ", lon=" + lon + ", lat=" + lat);
   });
   

</script>

Now if the chain of the JSON that you put of example, is the one that you are using really. Notice that it is badly armed, before id = 3 there is a # sign that should not be there, this should be replaced by },{ to make it a valid JSON fix.

    
answered by 26.12.2016 в 17:44