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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题