解析JSON无法获得值

时间:2017-11-06 14:29:03

标签: php html json html5

我的PHP cote返回错误:

  

未定义属性:stdClass :: $ paging in试图获取非对象的属性   使用未定义的常量total_items - 假设' total_items'在

如何获得total_items值?

这是JSON:

{
    "paging": {
        "total_items": 0,
        "current_page": 1,
        "total_pages": 0
     },
     "data": [],
     "facets": null
}
$content = file_get_contents($urll); 
$clean_content = removeBomUtf8($content); 
$decoded = json_decode($clean_content); 

foreach ($decoded->data as $data) 
{ 
    $total_items = (string)$data->paging[0]->total_items; 
    echo total_items; 
} ;

1 个答案:

答案 0 :(得分:1)

是的。

$data ='{"paging":{"total_items":0,"current_page":1,"total_pages":0},"data":[],"facets":null}';

$v = json_decode($data,true);
echo $v['paging']['current_page'];