json and php question
I am using the following in php to return the driving distance details between 2 locations:
$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = @file_get_contents($url);
A JSON object is returned, i am not familiar with JSON can somebody开发者_高级运维 please demonstrate how I can get the "Meters" element from this object into a string.
Thanks in advance.
Use json_decode
:
$obj = json_decode($data);
echo $obj->Directions->Distance->meters;
You might want to look at the json_decode()
function...
$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = json_decode(@file_get_contents($url));
echo $data->Directions->Distance->meters." meters";
Result:
123561 meters
Use json_decode
. JSON Decode
$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = @file_get_contents($url);
$obj = json_decode($data);
print $obj->meters;
Try this online json Viewer you can call JSON.parse in javascript to parse the string php output to a javascript object.
var json = {}; // parsed json object
var meters = json.Directions.Distance.meters; // is '123561'
精彩评论