循环通过嵌套的JSON

时间:2014-06-29 21:23:54

标签: php jquery html json

我有JSON:

{
    "PaperName": "Mathematics",
    "Instruction": null,
    "MaxTime": "2 hr",
    "Maxmarks": 100,
    "Question": [
        {
            "Type": "Objective",
            "QuesDetails": " Terms in an expression which have the same literal factors are called",
            "Marks": 1,
            "Order": 2,
            "Description": "This is the question Descripttion",
            "view": "View Answer",
            "Answer": [
                {
                    "Ans": "(A) Bhagat Singh"
                },
                {
                    "Ans": "(B) B.R.Ambedkar"
                },
                {
                    "Ans": "(C) J.L.Nehru"
                },
                {
                    "Ans": "(D) L.K.Advani."
                }
            ]
        },
        {
            "Type": "Objective",
            "QuesDetails": "An algebraic expression of two terms is known as",
            "Marks": 1,
            "Order": 2,
            "Description": "This is the question Descripttion",
            "view": "View Answer",
            "Answer": [
                {
                    "Ans": "(A) Bhagat Singh"
                },
                {
                    "Ans": "(B) B.R.Ambedkar"
                },
                {
                    "Ans": "(C) J.L.Nehru"
                },
                {
                    "Ans": "(D) L.K.Advani."
                }
            ]
        },
        {
            "Type": "Objective",
            "QuesDetails": "An algebraic expression of three terms is known as",
            "Marks": 1,
            "Order": 2,
            "Description": "This is the question Descripttion",
            "view": "View Answer",
            "Answer": [
                {
                    "Ans": "(A) Bhagat Singh"
                },
                {
                    "Ans": "(B) B.R.Ambedkar"
                },
                {
                    "Ans": "(C) J.L.Nehru"
                },
                {
                    "Ans": "(D) L.K.Advani."
                }
            ]
        }
    ]
}

现在我想将其转换为HTML。

我尝试通过以下代码:

var str="";
for(var item in json.Question){
str += "<tr>";
for(idata in json.Question[item]){
       str += '<td>'+json.Question[item][idata]+'</td>';

    }
  str+="</tr>";
 }
 $('#result').html("<table><th>Type</th><th>Ques Detail</th><th>Marks</th>
 <th>Order</th><th>Description</th><th></th><th>Answer</th>"+str+"</table>"); 

Bur for answer key我的输出为[Object][Object]。请让我知道如何访问它并以HTML格式显示。

3 个答案:

答案 0 :(得分:0)

json.Question.forEach(function(objective){
    console.log(objective);
    objective.Answer.forEach(function(answer){
        console.log(answer.Ans);
    });
});

Demo

如果有帮助,请告诉我。

编辑:打印一些HTML。

$("#myTable")
    .append("<tr><td>PaperName: " + json.PaperName + "</td></tr>")
    .append("<tr><td>Instructions: " + json.Instructions + "</td></tr>")
    .append("<tr><td>MaxTime: " + json.MaxTime + "</td></tr>")
    .append("<tr><td>Maxmarks: " + json.Maxmarks + "</td></tr>");
json.Question.forEach(function(objective){
    $("#myTable").append("<tr><td>Objective type: " + objective.Type + "</td></tr>");
    // etc
    objective.Answer.forEach(function(answer){
        $("#myTable").append("<tr><td>Answer: " + answer.Ans + "</td></tr>");
    });
});

Updated demo

答案 1 :(得分:0)

在你的json上运行json_decode然后:

$j = json_decode($json);

foreach($j->Question as $q)
{
    echo $q->QuesDetails;
    echo '<ul>';
    foreach($q->Answer as $ans)
    {
        echo '<li>' . $ans->Ans . '</li>';
    }
    echo '</ul>';
}

<强> Demo

答案 2 :(得分:0)

answers数组是一个对象数组。 所以

Json = {
Question = "Question"
Item = "Answer"
idata = "Ans": "(A) Bhagat Singh"
idata.ans = "(A) Bhagat Singh"

所以你要么需要一个for循环,要么需要将数组更改为字符串数组 示例

&#34;回答&#34;:[
&#34;(A)Bhagat Singh&#34;,
    &#34;(B)B.R.Ambedkar&#34;,
    &#34;(C)J.L.Nehru&#34;,
    &#34;(D)L.K.Advani。&#34;
    ]