我有JSON输入如下:
$json='{"PollQuestion":"Which is biggest country","Isactive":1,"options":[{"option":"abc"},{"option":"xyz"},{"option":"acssr"}]}'
$obj=json_encode($json);
那么如何访问选项字段和个别选项?我想把这些选项放在选项表中。请帮我解析php(Yii框架)中的这个json输入。
答案 0 :(得分:3)
$json='{"PollQuestion":"Which is biggest country","Isactive":1,"options":[{"option":"abc"},{"option":"xyz"},{"option":"acssr"}]}';
$decode = json_decode($json, true);
echo $decode['PollQuestion'];
答案 1 :(得分:2)
if you want to access as stdClass
$obj=json_decode($json);
print_r($obj->options);
if you want to access as hash array
$obj=json_decode($json,true);
print_r($obj['options']);
答案 2 :(得分:2)
Yii有一个帮助类CJSON,它包含一个方法CJSON::decode,用它来解码json字符串,并将解码后的值用作数组而不是对象。
希望有所帮助