dongxie8856 2014-02-01 10:03
浏览 41
已采纳

在PHP中解析json字符串

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "New Delhi",
               "short_name" : "New Delhi",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "West Delhi",
               "short_name" : "West Delhi",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Delhi",
               "short_name" : "DL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "New Delhi, Delhi, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 28.88981589999999,
                  "lng" : 77.34181459999999
               },
               "southwest" : {
                  "lat" : 28.4010669,
                  "lng" : 76.8396999
               }
            },
            "location" : {
               "lat" : 28.635308,
               "lng" : 77.22496
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 28.88981589999999,
                  "lng" : 77.34181459999999
               },
               "southwest" : {
                  "lat" : 28.4010669,
                  "lng" : 76.8396999
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

How do i parse the above json string with php

Trying the following code out but no success:

   $url = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=Delhi&sensor=true");


     $json = json_decode($url);


foreach($json->results as $item) {
        $location_item = array(
            'address' => $item->formatted_address,

        );

    }

I am getting blank page the code doesn't work please help i am new to php

  • 写回答

1条回答 默认 最新

  • duan97689 2014-02-01 10:27
    关注

    I am getting blank page the code doesn't work please help i am new to php

    Your code actually works , maybe you are not printing the data that is why you are getting a blank page.

    Tested

    <?php
    $url = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=Delhi&sensor=true");
    $json = json_decode($url);
    foreach($json->results as $item) {
        $location_item = array(
            'address' => $item->formatted_address,
    
        );
    
    }
    
    print_r($location_item);
    

    OUTPUT :

    Array
    (
        [address] => New Delhi, Delhi, India
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机