解析JSONArray jQuery中
我创建一个web应用程序,我可以使用谷歌地图API获得距离和运行时间..解析JSONArray jQuery中
https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999
我使用的HTTP方法,通过谷歌.. 其返回JSON作为给定格式下面给出
{
"destination_addresses" : [ "Kanyakumari - Panvel Highway, Palathara, Kerala 676501, India" ],
"origin_addresses" : [ "Kanyakumari - Panvel Highway, Randathani, Kerala 676510, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "2.5 km",
"value" : 2526
},
"duration" : {
"text" : "4 mins",
"value" : 228
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我怎么能使用jQuery提取文本的距离和持续时间文本和提醒吧..
使用file_get_contents()
和json_decode()
输出..
$contents = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999');
$data = json_decode($contents,true);
echo 'Distance : '.$data['rows'][0]['elements'][0]['distance']['text'];
echo "<br>";
echo 'Duration : '.$data['rows'][0]['elements'][0]['duration']['text'];
这会给你:
Distance : 2.5 km
Duration : 4 mins
是的,它的工作..谢谢兄弟.. –
欢迎兄弟@KarthikCP很高兴帮助你:)快乐编码 –
我可以转换时间秒到这里.. –
你有什么已经尝试过? – haxtbh
使用'jQuery.parseJSON()'。 – mnme