如何用PHP阅读这个JSON?

时间:2015-11-30 08:37:48

标签: php json

我从API调用中获取此JSON:

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

它包含在名为$data的变量中。当我调试时,它告诉我region.regions是一个数组,但我不能这样使用它。

这就是我将数据读入我的var:

的方法
$data = json_decode($jsonStringFromApiCall, true);

请帮助,谢谢!

3 个答案:

答案 0 :(得分:1)

希望这可以帮到你..

$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>";

}
}

答案 1 :(得分:0)

$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;

答案 2 :(得分:0)

$data = json_decode($jsonStringFromApiCall, true);

您现在可以使用$data['region.regions']

获取数据