PHP JSON值检索错误

PHP JSON值检索错误,php,jquery,json,Php,Jquery,Json,从我的PHP编码的JSON文件中检索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']

从我的PHP编码的JSON文件中检索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",
        "answer": "This is first answer"
    }
}, {
    "Ans": {
        "aid": "27",
        "answer": "This is second answer"
    }
}, {
    "Ans": {
        "aid": "28",
        "answer": "This is third"
    }
}]
  • 这个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);
                    });
                });
            }   
        });
    });
    
    $(文档).ready(函数(){
    $.ajax({
    类型:“POST”,
    cache:false,
    数据类型:“json”,
    url:'data.php',
    成功:功能(数据){
    $('.show_divis')。每个(函数(索引、值){
    var数据_票数=“”;
    数据投票+=''+数据[索引].q\U标题+'';
    数据投票+=''+数据[索引].q\U问题+'';
    $(this).html(数据投票).fadeOut(300).fadeIn(400);
    $('.show_divisis2')。每个(函数(索引、值){
    var数据_votes2='';
    数据_votes2+=''+数据[索引].Ans.aid+'';
    数据_votes2+=''+数据[索引].Ans.answer+'';
    $(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",
            "answer": "This is first answer"
        },
        {
            "aid": "27",
            "answer": "This is second answer"
        },
        {
            "aid": "28",
            "answer": "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]["answer"] = $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="answer_'+answers.aid+'">'+answers.answer+'</div>'; 
                    });
                    data_votes += '</div>';
                });
                // Add your reference to your desired html element instead of "BODY"
                $('body').append(data_votes);
            }
        });
    });
    
    $(文档).ready(函数(){
    $.ajax({
    类型:“POST”,
    cache:false,
    数据类型:“json”,
    url:'data.php',
    成功:功能(数据){
    var数据_票数=“”;
    $。每个(数据、功能(索引、问题){
    //控制台日志(问题);
    数据_投票+='';
    数据投票+=''+问题。q_标题+'';
    数据投票+='

    '+问题。问题+'

    '; $。每个(问题、答案、功能(索引、答案){ //控制台日志(应答); 数据投票+=''+答案。答案+''; }); 数据_投票+=''; }); //将引用添加到所需的html元素,而不是“BODY” $('body')。追加(数据投票); } }); });
    您使用的格式肯定不方便,因为顶级数组中的位置决定了答案所属的问题。将所有答案捆绑在所属问题内的一个数组中更符合逻辑。然后,您可以在javascript中使用两个嵌套循环,就像您在php中所做的一样。您好,谢谢您的回复。HTML部分只有2个div,这些类很好,你能帮我一点Jquery部分的忙吗?我已经为你投票了这个答案
    showdivis
    showdivis2
    的结构是什么?你能在你的问题中找到这些HTML吗?我能够理解为什么您要通过
    .each()
    迭代这些div,而不是迭代从AJAX返回的数据……告诉我最好的方法。我想在两个不同的栏目中展示,回答下面的问题你能帮我翻译一下Jquery代码吗?我卡住了