Rick Weller
Rick Weller

Reputation: 1258

parse json in javascript

I load a php/json file. This is my json file:

echo '{';
echo '"position":[';
while($inhoud = mysql_fetch_array($result))
{
  echo '{';
  echo '"lat":"'.$inhoud['lat'].'",';
  echo '"long":"'.$inhoud['long'].'",';
  echo '}';

}

echo ']}';

This works. I want to load it in my javascript and do it like this:

$.getJSON('req/position.php', function(data) { 
$.each(data, function(key, val) {
    newLatLng = key;
});
});

but this doesn't work. It loads the file but i don't get this data. What should i do?

Thanks,

Upvotes: 0

Views: 267

Answers (5)

anson
anson

Reputation: 4164

Constructing JSON in php through strings works but is primitive. Start constructing an array and use json_encode().

$arr = array();

while($inhoud = mysql_fetch_array($result)){
  $temp = array();
  $temp['lat'] = $inhoud['lat'];
  $temp['long'] = $inhoud['long'];
  $arr[] = $temp;
}

echo json_encode($arr);

If all that you select in your mysql query is 'lat' and 'long' you could also just place $inhoud into $arr inside your while loop like so.

while($inhoud = mysql_fetch_array($result)){      
  $arr[] = $inhoud;
}

If you do this just make sure you only select columns in your mysql query that you would want to output in JSON.

Upvotes: 1

lorenzo-s
lorenzo-s

Reputation: 17010

I think you have some syntax errors in the JSON output.

  • When you output "long" data, you append a comma , at the end, but you should not, because "long" is the last key of the object.
  • You print out an object in a while cycle. These objects are part of an array. So, except for the last one, you have to append a comma , after the closing }.

And, if I can ask, why you are not using the json_encode() PHP function, instead of build all the JSON string manually? With it, you build all the data as normal PHP array, and then encode (translate) it in JSON. You will avoid all these annoying syntax stuffs.

Just try it:

$data = array();
$data['position'] = array();

while($inhoud = mysql_fetch_array($result)) 
{
    $data['position'][] = array(
        "lat" => $inhoud['lat'],
        "long" => $inhoud['long']
    );
}

echo json_encode($data);

Upvotes: 5

Bartosz Grzybowski
Bartosz Grzybowski

Reputation: 1179

Return proper header in PHP script header('Content-type: application/json'); And it should work.

Also use json_encode to encode PHP values into valid JSON.

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24661

$.getJSON('req/position.php', function(data) {
   var obj = JSON.parse(data);
   // At this point, obj is an object with your JSON data.
});

Source: MDN

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245399

You have your co-ordinates defined in the array named position. You need to iterate through that. The Array contains objects with the properties lat and long. If you want to use the values, you should try something like:

$.getJSON('req/position.php'), function(data){
    $.each(data.position, function(index, value){
        var newLatLng = { latitude: value.lat, longitude: value.long };
    });
});

Upvotes: 1

Related Questions