ds19891231 2016-08-25 09:52
浏览 23
已采纳

在Jquery中解析JSON数组

I am Creating a webapp where i could get the distance and time of travel using google map API..

https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999

I am using HTTP method as given by google.. Its returning JSON Format as give below

{
   "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"
}

How could i extract the distance text and duration text using jquery and alert it..

  • 写回答

1条回答 默认 最新

  • drxv39706 2016-08-25 10:14
    关注

    Use file_get_contents() and json_decode() the output..

    $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'];
    

    This will give you :

    Distance : 2.5 km
    Duration : 4 mins
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?