JSON解析多个对象

I need to extract below fields from JSON, cant able to traverse some objects.

jSON (https://api.myjson.com/bins/19l74k)

Here is loop code

$results = json_decode($json,true);

        foreach($results['data'] as $res){
            echo $res['message']."<br/><br/>";
            echo $res['shares']."<br/><br/>";
            echo "<hr/>";

        }

Need to extract Shares Count, Comments(total_count),reactions(total_count). I tried looping shares with ['data'] but undefined index issue.

php

2个回答

There is an array in the data as well that needs to be accounted for -

Perhaps this will help

<?php

$f=file_get_contents("https://api.myjson.com/bins/19l74k");
$dataArray=json_decode($f,true);
$dataObject=json_decode($f);

print("By array reference: ".$dataArray['data'][0]['shares']['count']."
");
print("By object reference: ".$dataObject->data[0]->shares->count."
");

?>

Try this one,

$data = '{"data":[{"id":"123","created_time":"2018-10-04T11:51:28+0000","link":"link","picture":"picture","message":"message","shares":{"count":2},"comments":{"data":[],"summary":{"order":"ranked","total_count":0,"can_comment":true}},"reactions":{"data":[],"summary":{"total_count":37,"viewer_reaction":"NONE"}}},{"id":"2918","created_time":"2018-10-04T11:47:36+0000","link":"hss","picture":"aasd","message":"asdsa","shares":{"count":14},"comments":{"data":[],"summary":{"order":"ranked","total_count":0,"can_comment":true}},"reactions":{"data":[],"summary":{"total_count":227,"viewer_reaction":"NONE"}}}]}';
$output = json_decode($data, TRUE);

foreach ($output['data'] as $key => $value) {
    echo $value['message'].'<br/>';
    echo $value['shares']['count'].'<br/>';
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐