PHP JSON值检索错误


PHP JSON value retrieval error

我有一个问题,检索JSON值从我的PHP编码JSON文件

这是我的PHP代码:

$i = 0;
$qinfo = '';
$j=0;
$qry = "SELECT qid, q_title, q_question FROM questions WHERE qid = 1";
$result = mysql_query($qry);
$data = array();
while ($r = mysql_fetch_array($result)) {
    $qinfo[$i]['qid'] = $r['qid'];
    $qinfo[$i]['q_title'] = $r['q_title'];
    $qinfo[$i]['q_question'] = $r['q_question'];
    $qry2 = "SELECT aid, answer FROM answers WHERE qid=".$r['qid']." ";
    $result2 = mysql_query($qry2);
    while ($r2 = mysql_fetch_array($result2)) {
        $qinfo[$j]["Ans"]["aid"] = $r2['aid'];
        $qinfo[$j]["Ans"]["aid"] = $r2['answer'];
        $j++;
    }PHP
    $i++;
}
echo json_encode($qinfo);

输出JSON:

[{
    "qid": "1",
    "q_title": "This is first question title",
    "q_question": "This is first question description",
    "Ans": {
        "aid": "26",
        "answerswer": "This is first answer"
    }
}, {
    "Ans": {
        "aid": "27",
        "answerswer": "This is second answer"
    }
}, {
    "Ans": {
        "aid": "28",
        "answerswer": "This is third"
    }
}]
  1. JSON格式正确吗?简单的解释:我得到一个问题的答案。

这里这是jQuery代码我试图得到的结果。

$( document ).ready(function() {
    $.ajax({   
        type: "POST",
        cache: false,  
        dataType:"json",
        url: 'data.php',  
        success: function(data){
            $('.show_divis').each(function (index, value){
                var data_votes = '';
                data_votes += '<div style="color:#000">'+data[index].q_title+'</div>'; 
                data_votes += '<div style="color:#555">'+data[index].q_question+'</div>'; 
                $(this).html(data_votes).fadeOut(300).fadeIn(400);
                $('.show_divis2').each(function (index, value){
                    var data_votes2 = '';
                    data_votes2 += '<div style="color:#000">'+data[index].Ans.aid+'</div>'; 
                    data_votes2 += '<div style="color:#555">'+data[index].Ans.answer+'</div>'; 
                    $(this).html(data_votes2).fadeOut(300).fadeIn(400);
                });
            });
        }   
    });
});

将正确显示问题标题和描述。但只显示一个答案?根据我的JSON文件有3个答案。我想在这个问题下给出3个答案。我可以更改JSON格式吗?提前感谢!

你的JSON是有效的,但格式是不正确的,因为它应该在一个节点下有所有的答案,像这样:

[{
    "qid": "1",
    "q_title": "This is first question title",
    "q_question": "This is first question description",
    "Ans": [{
        "aid": "26",
        "answerswer": "This is first answer"
    },
    {
        "aid": "27",
        "answerswer": "This is second answer"
    },
    {
        "aid": "28",
        "answerswer": "This is third"
    }]
}]

要获得以上JSON格式,请更改PHP代码如下:

$i = 0;
$qinfo = array();
$qry = "SELECT qid, q_title, q_question FROM questions WHERE qid = 1";
$result = mysql_query($qry);
while ($r = mysql_fetch_array($result)) {
    $qinfo[$i]['qid'] = $r['qid'];
    $qinfo[$i]['q_title'] = $r['q_title'];
    $qinfo[$i]['q_question'] = $r['q_question'];
    $qry2 = "SELECT aid, answer FROM answers WHERE qid=".$r['qid']." ";
    $result2 = mysql_query($qry2);
    $j = 0;
    while ($r2 = mysql_fetch_array($result2)) {
        $qinfo[$i]["Ans"][$j]["aid"] = $r2['aid'];
        $qinfo[$i]["Ans"][$j]["answerswer"] = $r2['answer'];
        $j++;
    }
    $i++;
}
echo json_encode($qinfo);

和jQuery部分:

$( document ).ready(function() {
    $.ajax({   
        type: "POST",
        cache: false,  
        dataType:"json",
        url: 'data.php',  
        success: function(data){
            var data_votes = '';
            $.each(data, function (index, questions){
                //console.log(questions);
                data_votes += '<div style="display: block; background-color: #eee; margin: 5px 0px; padding: 5px;">';
                data_votes += '<h2 style="padding: 5px; margin: 0px;">'+questions.q_title+'</h2>'; 
                data_votes += '<p style="padding: 5px;">'+questions.q_question+'</p>'; 
                $.each(questions.Ans, function (index, answers){
                    //console.log(answers);
                    data_votes += '<div style="color:#555; padding: 5px; margin: 2px 0px; background-color: #ccc;" id="answerswer_'+answers.aid+'">'+answers.answer+'</div>'; 
                });
                data_votes += '</div>';
            });
            // Add your reference to your desired html element instead of "BODY"
            $('body').append(data_votes);
        }
    });
});