2013-02-23 1 views
0

Ниже приведен соответствующий код в моем приложении .....PHP Decode из Google Geocode API JSON Перерывы Когда ответ Содержит "/"

<? 
$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 ответ от Geocoder API Google (один из примеры, вспыхнувшие)

{ 
    "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--- 
} 

проблема, как представляется, произойдет соседством «Oakland/Winchell», моя теория состоит в том, что она содержит «/», который, как представляется, сделать его не вернуть ничего ... Как я это исправить ?

+0

«/' должно быть экранировано. У PHPs json_decode, как правило, нет проблем с ними. 'print_r()' все, что у вас есть. – mario

ответ

-1
$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