$json = file_get_contents('http://api.geonames.org/findNearbyPlaceNameJSON?lat=51.9877644&lng=-1.47866&username=demo');
$data = json_decode($json,true);
$Geonames = $data['geonames'][0];
echo "<pre>";
print_r($Geonames);
exit;
$json = '{"a":1,"b":2,"c":3}';
var_dump(json_decode($json)); //converts json to array
$array = [ "a" => 1, "b" => 2, "c" => 3];
echo json_encode($json_encode($json)); //converts array to json
<?php
$json = '{"firstName":"Peter","lastName:":"Silva","age":23}';
$personInfo = json_decode(json);
echo $personInfo->age;
?>
/** Checks if JSON and returns decoded as an array, if not, returns false,
but you can pass the second parameter true, if you need to return
a string in case it's not JSON */
function tryJsonDecode($string, $returnString = false) {
$arr = json_decode($string);
if (json_last_error() === JSON_ERROR_NONE) {
return $arr;
} else {
return ($returnString) ? $string : false;
}
}
$obj = json_decode("{string:'string'}");