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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么