0

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,

2
  • Define "this doesn't work". What happens that's different from what you expect to happen? What error messages do you receive, if any? What does the javascript console output in your browser when you try to accomplish whatever it is that "doesn't work?" Commented Feb 20, 2012 at 16:06
  • well, nothing. It loads the file and then nothing. I add an alert but this didn't fire Commented Feb 20, 2012 at 16:25

5 Answers 5

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

i changed my json to this code. I was not aware of the json_encode. Really nice! Thanks. It didn't fixed the script though but i am happy with this clean way
1

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 };
    });
});

1 Comment

i do this: $.getJSON('req/position.php'), function(data){ $.each(data.position, function(index, value){ alert('success'); }); }; but i don't get an alert.
1

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.

Comments

1

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.

Comments

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

Source: MDN

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.