【发布时间】:2018-06-08 17:33:07
【问题描述】:
我对 API 从来没有信心。 我设法使用 jQuery 在网页中获取 API,但现在我需要使用 PHP 仅解码以下 json (https://maps.googleapis.com/maps/api/geocode/json?address=via%20pascal%2033/2%2042123%20reggio%20emilia&key=) 的位置 lat 和 lng 并将这两个数据作为变量检索:
{
"results" : [
{
"address_components" : [
{
"long_name" : "2",
"short_name" : "2",
"types" : [ "street_number" ]
},
{
"long_name" : "Via Blaise Pascal",
"short_name" : "Via Blaise Pascal",
"types" : [ "route" ]
},
{
"long_name" : "Reggio Emilia",
"short_name" : "Reggio Emilia",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Reggio nell'Emilia",
"short_name" : "Reggio nell'Emilia",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Provincia di Reggio Emilia",
"short_name" : "RE",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Emilia-Romagna",
"short_name" : "Emilia-Romagna",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italia",
"short_name" : "IT",
"types" : [ "country", "political" ]
},
{
"long_name" : "42123",
"short_name" : "42123",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Via Blaise Pascal, 2, 42123 Reggio Emilia RE,
Italia",
"geometry" : {
"location" : {
"lat" : 44.6666003,
"lng" : 10.5960504
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 44.6679492802915,
"lng" : 10.5973993802915
},
"southwest" : {
"lat" : 44.6652513197085,
"lng" : 10.5947014197085
}
}
},
"partial_match" : true,
"place_id" : "ChIJWaiGeqUcgEcR9l6qwloEWgo",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
最后,我需要的是能够向用户显示:
"纬度是 $lat,经度是 $lng"
谢谢
【问题讨论】:
-
我以前从未听说过“API 解码”这个词。如果您问如何在 PHP 中解码 JSON,答案是 json_decode()。
-
是的,这就是我的意思,我尝试过使用 json 解码,但我无法解决我的问题,我只是在后期解码,所以只需 json_decode($datalink)不适合我。顺便说一句,感谢您的更正。
-
解码 JSON 是必须的,如果您不使用
json_decode(),则必须找到 third-party JSON decoder 或自己滚动。