Json解码麻烦

时间:2012-02-25 02:23:46

标签: php json

这是我解码的json输出,

object(stdClass)#1 (3) { ["name"]=> string(34) "xxxxxxxxxxxxxxx" ["Status"]=> object(stdClass)#2 (2) { ["code"]=> int(200) ["request"]=> string(7) "geocode" } ["Placemark"]=> array(1) { [0]=> object(stdClass)#3 (5) { ["id"]=> string(2) "p1" ["address"]=> string(32) "xxxxxxxxxxxxxxx, USA" ["AddressDetails"]=> object(stdClass)#4 (2) { ["Accuracy"]=> int(8) ["Country"]=> object(stdClass)#5 (3) { ["AdministrativeArea"]=> object(stdClass)#6 (2) { ["AdministrativeAreaName"]=> string(2) "UT" ["Locality"]=> object(stdClass)#7 (3) { ["LocalityName"]=> string(4) "xxxxxxxxxxxxxxx" ["PostalCode"]=> object(stdClass)#8 (1) { ["PostalCodeNumber"]=> string(5) "xxxxxxxxxxxxxxx" } ["Thoroughfare"]=> object(stdClass)#9 (1) { ["ThoroughfareName"]=> string(7) "xxxxxxxxxxxxxxx" } } } ["CountryName"]=> string(3) "USA" ["CountryNameCode"]=> string(2) "US" } } ["ExtendedData"]=> object(stdClass)#10 (1) { ["LatLonBox"]=> object(stdClass)#11 (4) { ["north"]=> float(xxxxxxxxxxxxxxx) ["south"]=> float(xxxxxxxxxxxxxxx) ["east"]=> float(-111.7018448) ["west"]=> float(xxxxxxxxxxxxxxx) } } ["Point"]=> object(stdClass)#12 (1) { ["coordinates"]=> array(3) { [0]=> float(xxxxxxxxxxxxxxx) [1]=> float(xxxxxxxxxxxxxxx) [2]=> int(0) } } } } }

我可以使用

检索邮政编码(84058)
echo $geo->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber; 

但我在世界上如何检索这些对象?:

  1. [ “北”] =>浮动(40.2824822)
  2. [ “东”] =>浮动(-111.7018448)
  3. [0] =>浮动(-111.7032032)
  4. [1] =>浮动(40.2811332)
  5. 谢谢,我是新来的。

2 个答案:

答案 0 :(得分:1)

您将以与检索邮政编码完全相同的方式检索它。 分解它以便你能理解它:

// access the entire json object
$geo

// access the placemark object (which contains an array of data)
$geo->Placemark

// access the placemark object at index 0
$geo->Placemark[0]

// access the Extended Data object of the Placemark object
$geo->Placemark[0]->ExtendedData

等等。所以现在你应该能够弄清楚如何一直到达北,南,东和西的元素。

echo $geo->Placemark[0]->ExtendedData->LatLonBox->north;
echo $geo->Placemark[0]->ExtendedData->LatLonBox->south;

等...

答案 1 :(得分:0)

1.    $geo->Placemark[0]->ExtendedData->LatLonBox->north;
2.    $geo->Placemark[0]->ExtendedData->LatLonBox->east;

3.    $geo->Placemark[0]->Point->coordinates[0];
4.    $geo->Placemark[0]->Point->coordinates[1];

如果您尝试通过漂亮的打印机运行它,很容易看到:

object(stdClass)#1 (3) {
    ["name"]=> string(34) "xxxxxxxxxxxxxxx" ["Status"]=> object(stdClass)#2 (2) {
        ["code"]=> int(200) ["request"]=> string(7) "geocode"
    }
    ["Placemark"]=> array(1) {
        [0]=> object(stdClass)#3 (5) {
            ["id"]=> string(2) "p1" ["address"]=> string(32) "xxxxxxxxxxxxxxx, USA" ["AddressDetails"]=> object(stdClass)#4 (2) {
                ["Accuracy"]=> int(8) ["Country"]=> object(stdClass)#5 (3) {
                    ["AdministrativeArea"]=> object(stdClass)#6 (2) {
                        ["AdministrativeAreaName"]=> string(2) "UT" ["Locality"]=> object(stdClass)#7 (3) {
                            ["LocalityName"]=> string(4) "Orem" ["PostalCode"]=> object(stdClass)#8 (1) {
                                ["PostalCodeNumber"]=> string(5) "xxx"
                            }
                            ["Thoroughfare"]=> object(stdClass)#9 (1) {
                                ["ThoroughfareName"]=> string(7) "S 325 W"
                            }

                        }

                    }
                    ["CountryName"]=> string(3) "USA" ["CountryNameCode"]=> string(2) "US"
                }

            }
            ["ExtendedData"]=> object(stdClass)#10 (1) {
                ["LatLonBox"]=> object(stdClass)#11 (4) {
                    ["north"]=> float(xxx) ["south"]=> float(40.2797843) ["east"]=> float(-111.7018448) ["west"]=> float(xxxx)
                }

            }
            ["Point"]=> object(stdClass)#12 (1) {
                ["coordinates"]=> array(3) {
                    [0]=> float(xxxxx) [1]=> float(xxxx) [2]=> int(0)
                }

            }

        }

    }

}