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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?