JSON解码PHP帮助

时间:2011-07-05 14:25:10

标签: 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->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 = '';

    }

}

这是我需要工作的数组: http://www.thebigfishexperience.org.uk/sources/ajax/venue-json.php?location=london

我需要从数组中检索坐标值,并将它们放入我的实例变量中,如上所示。

2 个答案:

答案 0 :(得分:1)

我相信你需要:

 $this->latitude = $obj['Placemark'][0]['Point']['coordinates'][0];
 $this->longitude = $obj['Placemark'][0]['Point']['coordinates'][1];

答案 1 :(得分:0)

查看$obj的转储,找到您要查找的两个值。然后将它们引用到你的两个变量中。

$this->latitude = $obj->path->to->value->one;
$this->longitude = $obj->path->to->value->two;