简体   繁体   English

如何使用PHP读取此JSON?

[英]How to read this JSON with PHP?

I am getting this JSON from an API call: 我从API调用获取此JSON:

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

It is contained in a variable called $data . 它包含在名为$data的变量中。 When I debug, it tells me that region.regions is an array, but I cant use it as such. 当我调试时,它告诉我region.regions是一个数组,但是我不能这样使用它。

This is how I read the data into my var: 这就是我将数据读入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']获取数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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