开发者

How to read this JSON using PHP?

I'm very new with JSON format, and in the tutorials I read I don't understand well how to parsed using php. So I have this:

{
    "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
            }
        }
    ]
}

And I want to echo coordinates and reverseGeocode. Can anyone please put me in the right direction?


Use 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);

Output:

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
                        )

                )

        )

)

The two properties you are looking for are then

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


Try running

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

where $json_string is your sample JSON as a string.


Code:

<?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);
?>

Output:

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
                        )

                )

        )

)

Now you can traverse the array as you wish.

Note: I reformatted your JSON so it's actually legible, using the JSONLint. And you should read this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜