我有如下 JSON 输入:
$json='{"PollQuestion":"Which is biggest country","Isactive":1,"options":[{"option":"abc"},{"option":"xyz"},{"option":"acssr"}]}'
$obj=json_encode($json);
那么如何访问选项字段和单个选项?我想把这些选项放在选项表中。请帮助我在 php(Yii 框架)中解析这个 json 输入。
$json='{"PollQuestion":"Which is biggest country","Isactive":1,"options":[{"option":"abc"},{"option":"xyz"},{"option":"acssr"}]}';
$decode = json_decode($json, true);
echo $decode['PollQuestion'];
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']);
Yii 有一个辅助类CJSON它包含一个方法CJSON::decode使用它来解码 json 字符串并将解码后的值用作数组而不是对象。
希望能帮助到你