Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP读取此JSON?_Php_Json - Fatal编程技术网

如何使用PHP读取此JSON?

如何使用PHP读取此JSON?,php,json,Php,Json,我对JSON格式非常陌生,在我阅读的教程中,我不太了解如何使用php解析。所以我有这个: { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [

我对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
            }
        }
    ]
}
我要回显坐标和反向编码。谁能帮我找到正确的方向吗?

试试跑步

$decoded = json_decode($json_string, true);
print_r($decoded);
print_r($decoded["features"][0]["geometry"]["coordinates"]);
echo $decoded["features"][0]["properties"]["reverseGeocode"];
其中,
$json\u string
是作为字符串的示例json。

尝试运行

$decoded = json_decode($json_string, true);
print_r($decoded);
print_r($decoded["features"][0]["geometry"]["coordinates"]);
echo $decoded["features"][0]["properties"]["reverseGeocode"];
其中,
$json\u string
是作为字符串的示例json。

使用:

您要查找的两个属性是

  • $obj->features[0]->几何体->坐标
  • $obj->features[0]->properties->reverseGeocode
使用:

您要查找的两个属性是

  • $obj->features[0]->几何体->坐标
  • $obj->features[0]->properties->reverseGeocode
代码:

<?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);
?>
现在,您可以根据需要遍历数组

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

代码:

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


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

这些变量有多个实例。你想要哪一个?这个“几何体”:{“类型”:“点”,“坐标”:[-67.593742,10.24462]},还有这个:“反向代码”:“委内瑞拉马拉凯”,它的
特征是
元素<代码>功能是一个数组。这些变量有多个实例。你想要哪一个?这个“几何体”:{“类型”:“点”,“坐标”:[-67.593742,10.24462]},还有这个:“反向代码”:“委内瑞拉马拉凯”,它的
特征是
元素<代码>功能
是一个数组。
<?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
                        )

                )

        )

)