json输出不如预期的那样

时间:2013-12-22 10:15:52

标签: php mysql ajax arrays

我有以下代码生成一个好的json输出,但它的开头和结尾分别有'['和']'。您还可以在

检查输出

http://android.ezinfotec.com/functions.php?method=getquestions

$query = mysql_query("select * from questions");
$i = 0;
        while($fetch = mysql_fetch_array($query))
        {

$output[] = array (
            "row".$i => array   (
                "id" => $fetch['id'],
                "answers" => $fetch['answers'],
                "status" => $fetch['ans_status'],
                "postedon" => substr($fetch['month'],0,3).' '.$fetch['day'].' '.$fetch['year'],
                "question" => $fetch['question'],
                "category" => $fetch['category'],
                "parent" => $fetch['parentcategory'],
                "authorid" => $fetch['author'],
                "authorname" => $fetch['author_name']   
                                )                   
                );
            $i++;           
        }
echo json_encode($output);

这是我的html页面,当我尝试从url获取数据时生成[Object object]

$(function() {
$('#getdata').click(function(){
        $.ajax({
        url: 'http://localhost:8080/android/functions.php',
        type : 'GET',
        data : 'method=getquestions',           
        dataType : 'json',
        success : function(s) {
            alert(s);
        },
        error: function(XMLHttpRequest,textStatus,errorThrown)
        {
            console.log(XMLHttpRequest+' '+textStatus+' '+errorThrown);
        }
    });
});

});

我认为这是因为'['和']'我无法使用ajax访问数据。在成功函数中我得到[对象对象]

3 个答案:

答案 0 :(得分:1)

好吧,我知道你必须将你的javascript更改为如下代码,并让你的PHP代码运行,因为它是你写的

success : function(s) {
                        for(var i =0 ;i < s.length; i++){
                              alert(s[i]["row"+i].id);
                              alert(s[i]["row"+i].answers);
                              alert(s[i]["row"+i].status);
                              alert(s[i]["row"+i].postedon);
                              alert(s[i]["row"+i].question);
                              alert(s[i]["row"+i].category);
                              alert(s[i]["row"+i].parent);
                              alert(s[i]["row"+i].authorid);
                              alert(s[i]["row"+i].authorname);
                       } 
                     }

答案 1 :(得分:1)

您的输出目前的格式为:

[
    {
        row0:
        {
            id: "17",
            answers: "0",
            status: "0",
            postedon: "Dec 4 2013",
            question: "How many courses within Aqeedah?",
            category: "91",
            parent: "89",
            authorid: "14",
            authorname: "Mehrakbar"
        }
    },
    {
        row1:
        {
            id: "18",
            answers: "0",
            status: "0",
            postedon: "Dec 10 2013",
            question: "what is jinn?",
            category: "97",
            parent: "89",
            authorid: "53",
            authorname: "Jack"
        }
    },
    etc
]

如果开头和结尾[]括号被切换为{}括号,那么您的输出将不再是有效的JSON。你希望输出看起来像什么?

如果是

{
    row0:
    {
        blah: blah
        blee: blah
    },
    row1:
    {
        blah: blah
        blee: blah
    }
}

您正在寻找,然后更改

$output[] = array (
        "row".$i => array   (

$output['row'.$i] = array (

并清理相应的parantheses。

答案 2 :(得分:0)

$query = mysql_query("select * from questions");
$i = 0;
$output = array(); // add this line