简体   繁体   中英

Getting values from JSON array

I'm using Mapquest in an attempt to get latitude and longitude values of a requested address.

This is my call to their API:

$json = file_get_contents('http://www.mapquestapi.com/geocoding/v1/address?key=******&callback=renderOptions&outFormat=json&inFormat=json&json={location:{street:"Lancaster,PA"},options:{thumbMaps:false}}');

$output = json_decode($json, true);

$lat = $output['results'][0]['locations'][0]['latLng']['lat'];
$lng = $output['results'][0]['locations'][0]['latLng']['lng'];

echo $lat.",".$lng;

As you can see, I'm trying to echo the LAT and LNG to screen but it's empty. When I print_r($json) , I get the following:

renderOptions({"results":[{"locations":[{"latLng":{"lng":-76.30127,"lat":40.03804},"adminArea4":"Lancaster County","adminArea5Type":"City"...............

So I know the response is OK and that LAT and LNG are available, but if I print_r($output) I get nothing and if I var_dump($output) I get NULL, and obviously I don't get my LAT LNG data.

I'm not great with JSON and arrays. Can somebody see what I'm doing wrong?

You need to remove &callback=renderOptions from the URL. Right now, the server returns JSON (with the specified callback function). (具有指定的回调函数)。 This is no valid JSON and so json_decode returns NULL (as specified in the manual)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM