3

I know that this question is already asked but I could not find answer for what I want.

I am trying to loop over data that I get in JSON data format after ajax response. my Javascript Code is:

function ajaxMe(){
    $(function(){
      $.ajax({
        type : 'GET',
        url : 's-test.php'
      }).done(function(data){
        values = [data];
        alert(data)
      })
    });
  }

After receiving the data in values variable successfully I am trying to print it using console.log(JSON.stringify(values)) in console and this gives result as:

["\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.320850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.420850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.520850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.620850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.720850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.820850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-0.920850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-1.320850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-2.320850\" , \"name\" : \"my corporation\"},\r\n  {\"lat\" : \"51.508742\", \"ln\" : \"-3.320850\" , \"name\" : \"my corporation\"}\r\n"]

instead of

[{"lat" : "51.508742", "ln" : "-0.320850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-0.420850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-0.520850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-0.620850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-0.720850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-0.820850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-0.920850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-1.320850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-2.320850" , "name" : "my corporation"},
  {"lat" : "51.508742", "ln" : "-3.320850" , "name" : "my corporation"}]

And unable to loop over this object when using forEach :

values.forEach(function(v, i){
    alert(i);
});

How it can work.? Thank you.

4
  • 3
    You are conveting the data to string. Try to use JSON.parse(data) to get the JSON object back. Commented Aug 25, 2016 at 12:06
  • If that is your data, looks like you server is not returning the data right Commented Aug 25, 2016 at 12:06
  • 1
    Another victim of the inexplicable JSON.stringify trend. Nowadays, we are flooded with people who convert a data structure (JSON) into a linear chain of characters (string) and then don't understand why they can't see their data structure anymore. I just can't explain how everyone got to do that. Commented Aug 25, 2016 at 12:07
  • instead of JSON.stringify I am unable to loop over the data and when I loop over the data it run only once and return whole object instead of running one by one. Commented Aug 25, 2016 at 12:13

3 Answers 3

5

First of all add a simple line to your jquery ajax code dataType: 'json'

$.ajax({
        type: 'GET',
        url: 's-test.php',
        dataType: 'json',
        success: function (data) {               
            $.each(data, function (key, value) {                     
                alert(value.lat);
            });               
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

2

try this:

function ajaxMe(){  
  $.ajax({
    method:"GET",
    url : 's-test.php',
    success:function(data){
      var obj = $.parseJSON(data);        
      $.each(obj, function (key, value){                     
          alert(value.lat);
          alert(value.ln);
          alert(value.name);
      });
    }
  });

}

Comments

1

When you echo the data from the PHP script, use json_encode(your data). This will return a JSON object. Then in the AJAX request set the 'dataType: "json"'. You will then be able to use console.log(data).

$.ajax({
    type : 'GET',
    url : 's-test.php'
    dataType: 'json',
  }).done(function(data){
    values = [data];
    alert(data)
  })

You will then be able to use a for loop to loop through the results like this:

for(var i = 0; i < data.legnth; i++){
    console.log(data[i]);
}

json_encode() documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.