dongsunny1113 2014-11-14 10:52
浏览 117
已采纳

通过JSON对象进行迭代

I apologize for the newbie question, but I'm trying I'm trying to iterate through JSON data without listing the specific objects within.

I'm specifying the JSON data first

$data = '{
  "question":[{
    "gender":"Both",
    "category":"Finance",
    "question_title":"Will the German Economy Collapse",
    "country":"Lebanon"
    }]
}';

I'm then decoding $data

$jsonDecoded = json_decode($data);

I'm then trying to iterate through the decoded JSON variable:

foreach ($jsonDecoded->question as $object){
    print $object;
}

But I'm getting the following error:

Catchable fatal error: Object of class stdClass could not be converted to string

Any pointers as to where I'm going wrong? Thank you.

  • 写回答

1条回答 默认 最新

  • dshfjsh_5455 2014-11-14 10:54
    关注
        <?php
    $data = '{
      "question":[{
        "gender":"Both",
        "category":"Finance",
        "question_title":"Will the German Economy Collapse",
        "country":"Lebanon"
        }]
    }';
    $jsonDecoded = json_decode($data);
    foreach ($jsonDecoded->question as $object){
        print_r ($object);
    }
    ?>
    

    like this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆
  • ¥15 jmeter出现403
  • ¥500 求华为P30PRO手机硬盘数据恢复