PHP的JSON解码帮助
我无法从已转换为数组的JSON对象中获取某些数据: 这是我的班级:PHP的JSON解码帮助,php,json,Php,Json,我无法从已转换为数组的JSON对象中获取某些数据: 这是我的班级: class tbfe_json_api { var $location; var $latitude; var $longitude; var $searchRadius = 20; // Default to 20 Miles var $reverseGeoCodeJSON; public function __construct() { $this->
class tbfe_json_api {
var $location;
var $latitude;
var $longitude;
var $searchRadius = 20; // Default to 20 Miles
var $reverseGeoCodeJSON;
public function __construct() {
$this->getLocationParams();
$this->getCoords( $this->reverseGeoCodeLocation( $this->location ) );
}
public function getLocationParams() {
if( isset( $_GET['location'] ) ) {
$this->location = str_replace(' ', '' , $_GET['location'] );
if( isset( $_GET['radius'] ) ) {
$this->searchRadius = $_GET['radius'];
}
}
else {
die('Invalid parameters specified for JSON request.');
}
}
public function reverseGeoCodeLocation($location) {
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, 'http://maps.google.com/maps/geo?q='.$location.'&output=json');
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
return $this->reverseGeoCodeJSON = curl_exec($cURL);
curl_close ($cURL);
}
public function getCoords($json_data) {
$obj = json_decode($json_data, true);
var_dump($obj);
// I NEED THE COORDINATE VALUES HERE TO PLACE BELOW
$this->latitude = '';
$this->longitude = '';
}
}
这就是我需要使用的阵列:
我需要从数组中检索坐标值,并将它们放入我的实例变量中,如上图所示。查看
$obj
的转储文件,找到要查找的两个值。然后将它们引用到两个变量
$this->latitude = $obj->path->to->value->one;
$this->longitude = $obj->path->to->value->two;
我相信你需要:
$this->latitude = $obj['Placemark'][0]['Point']['coordinates'][0];
$this->longitude = $obj['Placemark'][0]['Point']['coordinates'][1];
你有什么问题吗?这只是计算出数组的结构,然后相应地访问它的问题。类似于“
$obj['Placemark'][0]['Point']]…”
实际问题是什么?少了什么吗?还有,json字符串是什么样子的?