[英]How to read this JSON using PHP?
我是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 。 誰能請我朝正確的方向前進?
嘗試跑步
$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字符串。
使用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
碼:
<?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
)
)
)
)
現在,您可以根據需要遍歷數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.