谷歌的地理编码API JSON的PHP解码打破当响应包含“/”
问题描述:
下面是我的应用程序相关的代码.....谷歌的地理编码API JSON的PHP解码打破当响应包含“/”
<?
$jsonData = file_get_contents($url);
$data = json_decode($jsonData, TRUE);
$lat = $data['results']['0']['geometry']['location']['lat'];
$lng = $data['results']['0']['geometry']['location']['lng'];
$formattedAddress = $data['results']['0']['formatted_address'];
$acomp = $data['results']['0']['address_components'];
foreach ($acomp as $acompArray) {
if (in_array("neighborhood", $acompArray["types"])) {
$neighborhood = $acompArray["long_name"];
}
}
$acomp = $data['results']['0']['address_components'];
foreach ($acomp as $acompArray) {
if (in_array("neighborhood", $acompArray["types"])) {
$neighborhood = $acompArray["long_name"];
}
}
?>
下面是JSON响应从谷歌的地理编码API(的一个说破例子)
{
"results" : [
{
"address_components" : [
{
"long_name" : "3900",
"short_name" : "3900",
"types" : [ "street_number" ]
},
{
"long_name" : "Winchell Avenue",
"short_name" : "Winchell Ave",
"types" : [ "route" ]
},
{
"long_name" : "Oakland/Winchell",
"short_name" : "Oakland/Winchell",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Kalamazoo",
"short_name" : "Kalamazoo",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kalamazoo",
"short_name" : "Kalamazoo",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Michigan",
"short_name" : "MI",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "49008",
"short_name" : "49008",
"types" : [ "postal_code" ]
}
],
---A LOT MORE BUT DELETED THE IRRELEVANT PORTIONS---
}
这个问题似乎与所有的邻居“奥克兰/温切尔”的情况发生,我的理论是,它包含了这似乎让它返回任何“/” ...我如何解决这个问题?
答
$json_url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$row2[lat].",".$row2[lng]."&sensor=true";
// Initializing curl
$ch = curl_init($json_url);
// Setting curl options
curl_setopt_array($ch, $options);
// Getting results
$data = curl_exec($ch);
curl_close($ch);
// parse the json response
$jsondata = json_decode($data,true);
$results = $jsondata['results'][0];
$addr =$results['formatted_address'];
+0
上面的代码将允许您访问第一个具有地址完整描述的'formatted_address'字段...... – user1973273 2013-10-14 22:38:09
'/'应该被转义。然而PHP json_decode通常没有任何问题。 'print_r()'无论你有什么。 – mario 2013-02-23 20:14:39