PHP JSON值检索错误

时间:2016-11-16 08:27:46

标签: php jquery json

我从 PHP 编码的 JSON 文件中检索 JSON 值时遇到问题

这是我的 PHP 代码:

  list_pos  <- list() # create the list out of the for loop
  for(i in 1:100) {
    c=0.10 #colonization rate
    A=10 #Area of all islands(km^2)
    d=250 #Distance from host to target (A-T)
    s=0.1 #magnitude of distance
    d0=100 #Specific "half distance" for dispersal(km)
    C1 = c*A*exp(-d/d0) #Mainland to Target colonization
    Z =runif(1,0,1)
    x <- C1*A


    if(x <= Z) {
            list_pos[[i]]  <-  print("1") # Here you can store the 1 results.print is actually not necessary.
    } 
    if(x >= Z){
            list_pos[[i]]  <- print("0") # Here you can store the 0 results.print is actually not necessary.
    }
 } 

这是输出 JSON

$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);
  1. 这种JSON格式是否正确?简单解释一下:我得到1个问题的答案。
  2. 这是我试图获得结果的jQuery代码。

    [{
        "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"
        }
    }]
    

    它会正确显示问题标题和说明。但只显示1个答案?根据我的JSON文件,有3个答案。我想在这个问题下展示3个答案。我可以改变我的JSON格式吗? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

您的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);
        }
    });
});