简体   繁体   中英

How to read this JSON with PHP?

I am getting this JSON from an API call:

{"region.regions":[{"region":[{"name":"Trebbin","geoCodeId":1276004017004,"amount":0}]}]}

It is contained in a variable called $data . When I debug, it tells me that region.regions is an array, but I cant use it as such.

This is how I read the data into my var:

$data = json_decode($jsonStringFromApiCall, true);

Please help, thanks!

Hope this may help you..

$json = '{"region.regions":[{"region":[{"name":"Trebbin","geoCodeId":1276004017004,"amount":0}]}]}';
$data =  json_decode($json, true);

foreach ($data['region.regions'] as $region)
{
    foreach($region['region'] as $geo)
{
    echo "Name - ".$geo['name']."<br>";
    echo "Geo Code - ".$geo['geoCodeId']."<br>";
    echo "Amount - ".$geo['amount']."<br>";

}
}
$data='{"region.regions":[{"region":[{"name":"Trebbin","geoCodeId":1276004017004,"amount":0}]}]}';
$json=json_decode( $data, true );

$name=$json['region.regions'][0]['region'][0]['name'];
$geocode=$json['region.regions'][0]['region'][0]['geoCodeId'];
$amount=$json['region.regions'][0]['region'][0]['amount'];;

echo $name, ' ', $geocode,' ', $amount;
$data = json_decode($jsonStringFromApiCall, true);

您现在可以使用$data['region.regions']获取数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM