开发者

json and php question

开发者 https://www.devze.com 2023-02-02 11:34 出处:网络
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\';

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消