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

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

报告相同问题?

悬赏问题

  • ¥20 画CAD几张图片内容如下1111111111111111111111111111111111111111111
  • ¥15 基于python的电影系统推荐
  • ¥20 springmvc重定向和返回json
  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果