JSON.parse将JSON数据加载到PHP和Javascript中

时间:2014-07-24 08:31:42

标签: javascript php ajax json

我写了一个像这样的JSON文件

{
"q1":{
        "sn":"1.",
        "ques": "From the list of words lettered A to D choose the one that is <b>most nearly opposite in meaning</b> to the underlined word and that will, at the same time, correctly fill the gap in the sentence: The management has neither <u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u> nor <b><u>denied</u></b> the rumour that it was going to lay off some workers",
        "opt": "<br /><input type='radio' name='q1' /> pronounced <br /> <input type='radio' name='q1' /> confirmed <br /> <input type='radio' name='q1' /> rejected <br /> <input type='radio' name='q1' /> advertised"
    },

"q2":{
        "sn":"2.",
        "ques": "From the list of words lettered A to D choose the one that is <b>most nearly opposite in meaning</b> to the underlined word and that will, at the same time, correctly fill the gap in the sentence: The arrival of the police at the scene <u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u> rather than <b><u>mitigated</u></b> tension",
        "opt": "<br /><input type='radio' name='q2' /> provoked <br /> <input type='radio' name='q2' /> heightened <br /> <input type='radio' name='q2' /> created <br /> <input type='radio' name='q2' /> prolonged"
    },

"q3":{
        "sn":"3.",
        "ques": "From the list of words lettered A to D choose the interpretation that you consider <b>most appropriate</b> for this sentence: When I leave this country it will be for good. This means that i will",
        "opt": "<br /><input type='radio' name='q3' /> never come back <br /> <input type='radio' name='q3' /> leave for better conditions elsewhere <br /> <input type='radio' name='q3' /> become an adventurer <br /> <input type='radio' name='q3' /> improvement ways"
    },

"q4":{
        "sn":"4.",
        "ques": "From the list of words lettered A to D choose the word that <b>best completes</b> the following sentence: Because the new venture was <u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u> many businessman and women went into it.",
        "opt": "<br /><input type='radio' name='q4' /> rich <br /> <input type='radio' name='q4' /> manageable <br /> <input type='radio' name='q4' /> lucrative <br /> <input type='radio' name='q4' /> satisfying"
    },

"q5":{
        "sn":"5.",
        "ques": "From the list of words lettered A to D choose the one that is <b>nearly in meaning</b> to the underlined word and that will, at the same time, correctly fill the gap in the sentence: She was <b><u>reprimanded</u></b> by her boss for negligence",
        "opt": "<br /><input type='radio' name='q5' /> sacked <br /> <input type='radio' name='q5' /> rebuked <br /> <input type='radio' name='q5' /> punished <br /> <input type='radio' name='q5' /> surcharged"
    }
}               

我想创建一个基于在线计算机的考试。

我使用JSON.parse使用PHP file_get_contents(question.json)加载数据;

我的问题是我希望数据集逐个加载,例如q1首先显示然后当点击下一个按钮时q2应该显示直到它到达q5

我尝试使用AJAX将其推送到像这样的javascript数组

for(var obj in data) {
            //opt.arr.push(data[obj].opt);
            sn = data[obj].sn;
            ques = data[obj].ques;
            opt = data[obj].opt;
            //results.innerHTML += data[obj].sn+data[obj].ques+data[obj].opt+"<hr /><br />";
        }
        results.innerHTML += sn + ques + opt+"<hr /><br />";

我想创建一个像这样的基于计算机的测试 ![基于计算机的测试] [1]

stackoverflow不允许我上传图片,我会上传图片的样子以便更好地解释。

当onload时,json文件中的第一组问题,例如“q1”:应该用其选项集加载,当用户点击2或下一个“q2”时:json文件中的数据集应该加载并隐藏“q1”:json文件中的数据集,如果用户单击下面的3或下一个按钮,则只有json文件中的数据集“q3”:应该加载其选项集等等。 ..如果单击下一个按钮,则应加载下一组带有选项的问题。

我也很感激如何跟踪用户选择的答案并检查它是否正确。

有人可以让我通过。我对JSON有点新意,所以稍微放松一下。

1 个答案:

答案 0 :(得分:0)

由于我不确定您是否正在搜索基于JavaScript或PHP的解决方案,我将使用php。要将JSON数组转换为php数组,可以使用json_decode然后遍历数组以执行任何操作(设置变量,输出数据,...

$questions= json_decode($your_json_file);

foreach($questions as $questionObj) {
    echo $questionObj['sn'];
    echo $questionObj['ques'];
    echo $questionObj['opt'];
}