-1

Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?

I recived data by ajax

<?
 $locations[] = array(
'Name'=>$name,
'Latitude'=>$lat,
'Longitude'=>$long};
print_r(json_encode($locations));
?>

Here I have error, becasu it doesn't show anything when I tried with alert(data) and it work and show the array json that is below

success:function(data) {
  var dat =$.parseJSON(data);
   $("#pru").html(dat.Name); //here it doesn't show anything if I put alert(data) it show me all the array json
}

the array json content, the next array:

[{"Name":"Jayme jayden","Latitude":"36.712005","Longitude":"-4.43825"},
{"Name":"Jhonny","Latitude":"36.728744","Longitude":"-4.443822"},
{"Name":"Jessica Lynn","Latitude":"36.7418","Longitude":"-4.4333 "}]
Community
  • 1
  • 1
Kakitori
  • 901
  • 6
  • 16
  • 41

1 Answers1

3

You shouldn't use print_r to send JSON. Just do a regular echo:

$locations = array(
    'Name' => $name,
    'Latitude' => $lat,
    'Longitude' => $long
);

echo json_encode($locations);

You also have a syntax error in your code (the closing brace for the array), and I think you're unintentionally creating a multi-dimensional array. Use the code above, and it should work.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292