我有一个json代码,尝试了很多方法但仍无法从json获取数据
JSON:
echo $answer->option;
// will output
{"customer_ans":["qqq","sss"],
"show_image":["images/a.png","images/b.png"]},
{"customer_ans":["ooo","ooo","ooo"],
"show_image":["images/a.png","images/b.png","images/c.png"]}
...thesame...blablabla
当我尝试
时foreach($answer->option as $mydata)
{
foreach($mydata->customer_ans as $values)
{
echo $values . "\n";
}
}
没有输出,我试过
$ansopts = json_decode( $answer->option,true );
for($i=0;$i<count($ansopts["customer_ans"]);$i++) {
echo $ansopts["customer_ans"][$i];
}
仍然没有输出!
那么我怎样才能获得customer_ans值?
**********************************************更新*****************
for($i=0;$i<count($ansopts);$i++) {
echo var_export( $ansopts["customer_ans"][$i]);
}
没有输出
echo '<pre>'.print_r(json_decode($answer->option, true), true).'</pre>';
无输出
var_dump函数:
var_dump($answer->option);
// will output
string(291) "{"customer_ans":["aaa","bbb"],"show_image":["images/a.png","images/a.png"]},{"customer_ans":["ccc","ddd","eee"],"show_image":["images/a.png","images/b.png","images/c.png"]}"
非常感谢大家!
答案 0 :(得分:1)
以这种方式尝试:
#the json is a littel invalid to parse, missing [ ] around it
$ansopts = json_decode('['.trim((string)$answer->option).']',true );
//test for errors
print json_last_error_msg ();
//to see it for real
var_export($ansopts);
//loop
for($i=0;$i<count($ansopts);$i++) {
echo $ansopts[$i]["customer_ans"][0];#qqq
echo $ansopts[$i]["customer_ans"][1];#sss
}
答案 1 :(得分:0)
尝试
$ansopts = json_decode( $answer->option,true );
foreach($ansopts as $mydata)
{
foreach($mydata->customer_ans as $values)
{
echo $values . "\n";
}
}
或者您可以尝试先转储变量
print_r($ansopts);exit;