0

我正在尝试从 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" }
4

1 回答 1

0

JSON 格式不正确。在我这样做之后stripslashes,JSON 应该形成如下有效:

{
    "1": {
        "QID": "1",
        "Type": "MC",
        "Question": "Questionhere",
        "Options": {
            "1": "AnswerOpt1"
        }
    }
}
于 2013-06-25T21:18:15.837 回答