我正在尝试从 JSON 编码的字符串中解析一些选项数据。这是我已经拥有的代码:
<?php
$json = json_decode('{"1":"{\"QID\":\"1\",\"Type\":\"MC\",\"Question\":\"Question here\",\"Options\":{\"1\":\"Answer Opt 1\"}}"}');
foreach ($json as $QID => $Data) {
echo "QID: $QID, Type: ";
$new = json_decode(stripslashes($Data));
if($new->Type=="MC"){
echo "Multiple Choice, Question: ".$new->Question.", Options: ";
$options = json_decode($new->Options,true);
}else{
echo "Unknown";
}
echo ".<br/>";
}
?>
提供的 JSON 字符串是发送到脚本的内容,我得到的错误是:
json_decode() expects parameter 1 to be string, object given
$new->Options 的 VarDump 是:
object(stdClass)#3 (1) { ["1"]=> string(1) "2" }