[英]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. 我是JSON格式的新手,在阅读的教程中,我不太了解如何使用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 . 我想回显坐标和reverseGeocode 。 Can anyone please put me in the right direction?
谁能请我朝正确的方向前进?
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. 其中
$json_string
是示例JSON字符串。
Use json_decode : 使用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
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 . 注意:我使用JSONLint重新格式化了您的JSON,使它实际上清晰易读。 And you should read this .
而且你应该读这个 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.