doujiyun0041 2018-10-06 14:54
浏览 185

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.

  • 写回答

2条回答 默认 最新

  • dr6673999 2018-10-06 16:29
    关注

    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."
    ");
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线