Я очень новичок в формате 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 ) ) ) )
Теперь вы можете перемещать массив по своему усмотрению.
Примечание. Я переформатировал ваш JSON, чтобы он был наглядным , используя JSONLint . И вы должны это прочитать.