【问题标题】:How to read this JSON using PHP?如何使用 PHP 读取这个 JSON?
【发布时间】:2011-04-03 14:52:34
【问题描述】:

我对 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。谁能帮我指明正确的方向?

【问题讨论】:

  • 这些变量有多个实例。你想要哪些?
  • 这个“几何”:{“类型”:“点”,“坐标”:[-67.593742, 10.24462]},
  • 还有这个:“reverseGeocode”:“马拉凯,委内瑞拉”,
  • 对于哪个features 元素? features 是一个数组。

标签: php json


【解决方案1】:

使用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-&gt;features[0]-&gt;geometry-&gt;coordinates
  • $obj-&gt;features[0]-&gt;properties-&gt;reverseGeocode

【讨论】:

    【解决方案2】:

    尝试运行

    $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。

    【讨论】:

      【解决方案3】:

      代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        相关资源
        最近更新 更多