【问题标题】:How to Fetch google APi array如何获取谷歌 API 数组
【发布时间】:2014-09-30 10:11:57
【问题描述】:

我想从这个 google api 数组中获取 Postcode [ "long_name" : "S35 1TZ",]

我正在使用这个 API

http://maps.googleapis.com/maps/api/geocode/json?address=(53.4624118,%20-1.4922259999999596)

使用
msg.results[0].address_components[6].long_name;
不工作

{
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "7A",
                   "short_name" : "7A",
                   "types" : [ "street_number" ]
                },
                {
                   "long_name" : "Hollow Gate",
                   "short_name" : "Hollow Gate",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Chapeltown",
                   "short_name" : "Chapeltown",
                   "types" : [ "sublocality_level_1", "sublocality", "political" ]
                },
                {
                   "long_name" : "Sheffield",
                   "short_name" : "Sheffield",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Sheffield",
                   "short_name" : "Sheffield",
                   "types" : [ "postal_town" ]
                },
                {
                   "long_name" : "South Yorkshire",
                   "short_name" : "South York",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "United Kingdom",
                   "short_name" : "GB",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "S35 1TZ",
                   "short_name" : "S35 1TZ",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "7A Hollow Gate, Sheffield, South Yorkshire S35 1TZ, UK",
             "geometry" : {
                "location" : {
                   "lat" : 53.4624118,
                   "lng" : -1.492226
                },
                "location_type" : "ROOFTOP",
                "viewport" : {
                   "northeast" : {
                      "lat" : 53.4637607802915,
                      "lng" : -1.490877019708498
                   },
                   "southwest" : {
                      "lat" : 53.4610628197085,
                      "lng" : -1.493574980291502
                   }
                }
             },
             "types" : [ "street_address" ]
          }
       ],
       "status" : "OK"
    }

【问题讨论】:

  • 即使代码不正确,请注意6 应该是7

标签: php arrays google-maps


【解决方案1】:

有两种方法。根据您的代码,我不确定您是否获得了该数据的字符串或已经是一个对象。如果你只取回数据,它是一个字符串,你首先必须将 JSON 转换为 PHP 数组或对象。

1 - 您可以将 JSON 转换为数组:

$msg = json_decode($msg,true);

/* And call it by */
echo($msg['results'][0]['address_components'][7]['long_name']);

2 - 使用对象表示法:

$msg = json_decode($msg);

/* And call it by */
echo($msg->results[0]->address_components[7]->long_name);

如果您使用 Google API PHP 库取回数据,它可能不是字符串而是对象,因此您可以关闭 json_decode() 并使用选项 2。如果您更喜欢使用选项 1,您可以强制转换使用$msg = (array)$msg;将对象放入数组中

通常使用print_r($msg)var_dump($msg) 可以方便地调试事物,只需在每次接近目标时添加路径即可。所以在这种情况下是var_dump($msg),然后是var_dump($msg->result),然后是var_dump($msg->result[0]),以此类推。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多