通过JSON对象进行迭代

时间:2014-11-14 10:52:11

标签: php json iterator

我为新手问题道歉,但我正在努力 我试图迭代JSON数据而不列出其中的特定对象。

我先指定JSON数据

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

然后解码$ data

$jsonDecoded = json_decode($data);

然后我尝试迭代解码的JSON变量:

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

但我收到以下错误:

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

关于我出错的地方有什么指示?谢谢。

1 个答案:

答案 0 :(得分:0)

    <?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);
}
?>
像这样