0

I have an iteration trough json that I cannot make to work.

$.post('getdatafromdatabase.php', function(data) {
    var output="<ul> START ";

    for (var i in data.results) {
        output+="<li>" + data.results[i].id + " " + data.results[i].latitude + "--" + data.results[i].longitude+"</li>";
    }

    output+=" END </ul>" + data;
    document.getElementById("bounds").innerHTML += output;
});

The "START" and the "END" does print. Also after the "END" I am printing the "data" out which also works, so I know I am getting the data from the database and it looks like this:

{"results":[{"id":"1","latitude":"47.49882200000000","longitude":"19.05680300000000"},     {"id":"2","latitude":"47.49273300000000","longitude":"19.03345700000000"},{"id":"3","latitude":"47.47765200000000","longitude":"18.98942600000000"},{"id":"4","latitude":"47.47266300000000","longitude":"18.81643700000000"}]}

Can someone please help me with this. I spent a lot of time on this and I can't figure out why can't I get it to print. Thanks.

7
  • You don't "iterate though json". JSON is a textual representation of a javascript structure - it's just a STRING. You interate through a STRUCTURE, which is what the json becomes after it's decoded. e.g. you go through it like you would any other array/object in javascript. Commented Mar 10, 2014 at 4:47
  • alert data.results inside the function and see if it shows text or [object]. Commented Mar 10, 2014 at 4:50
  • Sorry for using the wrong expression. So how do I loop through the json string so it prints. I looked through my code a zillion times and can't find the problem. Commented Mar 10, 2014 at 4:50
  • May be you need to use $.getJSON instead of $.post (api.jquery.com/jquery.getjson) Commented Mar 10, 2014 at 4:51
  • I added alert(data.results); to the function and it says "undefined" Commented Mar 10, 2014 at 4:53

3 Answers 3

1

Use .ajax instead of $.post

$.ajax({url:'getdatafromdatabase.php',
        type: "POST",
        data: { name: "John", location: "Boston" }}).done(function(data) {
    var output="<ul> START ";
    for (var i in data.results) {
        output+="<li>" + data.results[i].id + " " + data.results[i].latitude + "--" + data.results[i].longitude+"</li>";
    }
    output+=" END </ul>" + data;
    document.getElementById("bounds").innerHTML += output;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thank you this works, but the problem is that I will need to post data to the server and have to use the post method.
0

Use

var data = $.parseJSON(data);

before you start looping to read.

2 Comments

Thank you. This solved my problem. Could you elaborate why do I need to parse the json data?
The reason is it is a STRING as marc's comment. You have get it to JSON structure before you parse it.
0

this way for iterating through json in javascript:

for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    var val = obj[key];
    console.log(val);
  }
}

Comments