如何使用PHP阅读此JSON?

时间:2011-04-03 14:52:34

标签: php json

我是JSON格式的新手,在我阅读的教程中,我不太清楚如何使用php解析。所以我有这个:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -67.593742,
                    10.24462
                ]
            },
            "properties": {
                "id": "669163449",
                "accuracyInMeters": 0,
                "timeStamp": 1301841780,
                "reverseGeocode": "Maracay, Venezuela",
                "photoUrl": "https://www.google.com/latitude/apps/badge/api?type=photo&photo=Df7VGy8BAAA.9of56owsf4wI6F4odEQ",
                "photoWidth": 96,
                "photoHeight": 96,
                "placardUrl": "https://g=true&stale=false&lod=4&format=png",
                "placardWidth": 56,
                "placardHeight": 59
            }
        }
    ]
}

我想要回显坐标 reverseGeocode 。任何人都可以把我放在正确的方向吗?

3 个答案:

答案 0 :(得分:3)

尝试运行

$decoded = json_decode($json_string, true);
print_r($decoded);
print_r($decoded["features"][0]["geometry"]["coordinates"]);
echo $decoded["features"][0]["properties"]["reverseGeocode"];

其中$json_string是您的样本JSON字符串。

答案 1 :(得分:3)

使用json_decode

$json = <<<EOD
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [-67.593742, 10.24462]},
"properties": {
"id": "669163449",
"accuracyInMeters": 0,
"timeStamp": 1301841780,
"reverseGeocode": "Maracay, Venezuela",
"photoUrl": "https://www.google.com/latitude/apps/badge/api?type=photo&photo=Df7VGy8BAAA.9of56owsf4wI6F4odEQ",
"photoWidth": 96,
"photoHeight": 96,
"placardUrl": "https://g=true&stale=false&lod=4&format=png",
"placardWidth": 56,
"placardHeight": 59
}
}
]
}
EOD;

$obj = json_decode($json);

print_r($obj);

输出:

stdClass Object
(
    [type] => FeatureCollection
    [features] => Array
        (
            [0] => stdClass Object
                (
                    [type] => Feature
                    [geometry] => stdClass Object
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => -67.593742
                                    [1] => 10.24462
                                )

                        )

                    [properties] => stdClass Object
                        (
                            [id] => 669163449
                            [accuracyInMeters] => 0
                            [timeStamp] => 1301841780
                            [reverseGeocode] => Maracay, Venezuela
                            [photoUrl] => https://www.google.com/latitude/apps/badge/api?type=photo&photo=Df7VGy8BAAA.9of56owsf4wI6F4odEQ
                            [photoWidth] => 96
                            [photoHeight] => 96
                            [placardUrl] => https://g=true&stale=false&lod=4&format=png
                            [placardWidth] => 56
                            [placardHeight] => 59
                        )

                )

        )

)

您正在寻找的两个属性是

  • $obj->features[0]->geometry->coordinates
  • $obj->features[0]->properties->reverseGeocode

答案 2 :(得分:1)

代码:

<?php
$json = <<<EOF
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -67.593742,
                    10.24462
                ]
            },
            "properties": {
                "id": "669163449",
                "accuracyInMeters": 0,
                "timeStamp": 1301841780,
                "reverseGeocode": "Maracay, Venezuela",
                "photoUrl": "https://www.google.com/latitude/apps/badge/api?type=photo&photo=Df7VGy8BAAA.9of56owsf4wI6F4odEQ",
                "photoWidth": 96,
                "photoHeight": 96,
                "placardUrl": "https://g=true&stale=false&lod=4&format=png",
                "placardWidth": 56,
                "placardHeight": 59 
            } 
        } 
    ] 
}
EOF;

$ar = json_decode($json, true);
print_r($ar);
?>

输出:

Array
(
    [type] => FeatureCollection
    [features] => Array
        (
            [0] => Array
                (
                    [type] => Feature
                    [geometry] => Array
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => -67.593742
                                    [1] => 10.24462
                                )

                        )

                    [properties] => Array
                        (
                            [id] => 669163449
                            [accuracyInMeters] => 0
                            [timeStamp] => 1301841780
                            [reverseGeocode] => Maracay, Venezuela
                            [photoUrl] => https://www.google.com/latitude/apps/badge/api?type=photo&photo=Df7VGy8BAAA.9of56owsf4wI6F4odEQ
                            [photoWidth] => 96
                            [photoHeight] => 96
                            [placardUrl] => https://g=true&stale=false&lod=4&format=png
                            [placardWidth] => 56
                            [placardHeight] => 59
                        )

                )

        )

)

现在您可以根据需要遍历数组。

注意:我使用JSONLint重新格式化了您的JSON,因此它实际上清晰可辨。你应该阅读this