简体   繁体   English

JSON 解码帮助与 PHP

[英]JSON Decode Help with PHP

I am having trouble getting some data from a JSON object which I have converted into an array:我无法从已转换为数组的 JSON object 获取一些数据:

This is my Class:这是我的 Class:

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 = '';

    }

}

And this is the array I need to work from: http://www.thebigfishexperience.org.uk/sources/ajax/venue-json.php?location=london这是我需要工作的数组: http://www.thebigfishexperience.org.uk/sources/ajax/venue-json.php?location=london

I need to retrieve the coordinate values from the array and place them into my instance variables as you see above.如上所示,我需要从数组中检索坐标值并将它们放入我的实例变量中。

i believe you need:我相信你需要:

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

Look at the dump of $obj and find the two values you're looking for.查看$obj的转储并找到您要查找的两个值。 Then reference them to your two variables.然后将它们引用到您的两个变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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