JSON 编码不正确的语法

JSON Encode Incorrect Syntax

我按照问题 7076525 的说明进行操作,但没有成功。我已经添加了我的 [] 但出于某种原因它只将它们放在每三行附近?我无法弄清楚要在我的代码中放入什么语法来完成这项工作。

    // output data of each row
    while($row = $result->fetch_assoc()) {

    $rows[] = $row;
    //Add the header...
    header('Content-Type: application/json');
        echo json_encode($rows);
    }
} else {
    echo "0 results";
}

这是我的 json 数据目前的样子:

[
    {
        "ticket_id": "66",
        "number": "000005",
        "user_id": "109",
        "user_email_id": "0",
        "status_id": "3",
        "dept_id": "1",
        "sla_id": "1",
        "topic_id": "36",
        "staff_id": "1",
        "team_id": "0",
        "email_id": "0",
        "flags": "0",
        "ip_address": "10.18.1.195",
        "source": "Phone",
        "isoverdue": "0",
        "isanswered": "1",
        "duedate": null,
        "reopened": null,
        "closed": "2015-04-20 07:16:46",
        "lastmessage": "2015-04-20 07:16:46",
        "lastresponse": "2015-04-20 07:16:46",
        "created": "2015-04-20 07:16:46",
        "updated": "2015-04-20 07:16:46"
    }
][
    {
        "ticket_id": "66",
        "number": "000005",
        "user_id": "109",
        "user_email_id": "0",
        "status_id": "3",
        "dept_id": "1",
        "sla_id": "1",
        "topic_id": "36",
        "staff_id": "1",
        "team_id": "0",
        "email_id": "0",
        "flags": "0",
        "ip_address": "10.18.1.195",
        "source": "Phone",
        "isoverdue": "0",
        "isanswered": "1",
        "duedate": null,
        "reopened": null,
        "closed": "2015-04-20 07:16:46",
        "lastmessage": "2015-04-20 07:16:46",
        "lastresponse": "2015-04-20 07:16:46",
        "created": "2015-04-20 07:16:46",
        "updated": "2015-04-20 07:16:46"
    },
    {
        "ticket_id": "67",
        "number": "000006",
        "user_id": "129",
        "user_email_id": "0",
        "status_id": "3",
        "dept_id": "1",
        "sla_id": "1",
        "topic_id": "36",
        "staff_id": "1",
        "team_id": "0",
        "email_id": "0",
        "flags": "0",
        "ip_address": "10.18.1.121",
        "source": "Other",
        "isoverdue": "0",
        "isanswered": "1",
        "duedate": null,
        "reopened": "2015-04-20 07:25:42",
        "closed": "2015-04-20 07:25:54",
        "lastmessage": "2015-04-20 07:18:33",
        "lastresponse": "2015-04-20 07:25:54",
        "created": "2015-04-20 07:18:33",
        "updated": "2015-04-20 07:25:54"
    }
][
    {
        "ticket_id": "66",
        "number": "000005",
        "user_id": "109",
        "user_email_id": "0",
        "status_id": "3",
        "dept_id": "1",
        "sla_id": "1",
        "topic_id": "36",
        "staff_id": "1",
        "team_id": "0",
        "email_id": "0",
        "flags": "0",
        "ip_address": "10.18.1.195",
        "source": "Phone",
        "isoverdue": "0",
        "isanswered": "1",
        "duedate": null,
        "reopened": null,
        "closed": "2015-04-20 07:16:46",
        "lastmessage": "2015-04-20 07:16:46",
        "lastresponse": "2015-04-20 07:16:46",
        "created": "2015-04-20 07:16:46",
        "updated": "2015-04-20 07:16:46"
    },
    {
        "ticket_id": "67",
        "number": "000006",
        "user_id": "129",
        "user_email_id": "0",
        "status_id": "3",
        "dept_id": "1",
        "sla_id": "1",
        "topic_id": "36",
        "staff_id": "1",
        "team_id": "0",
        "email_id": "0",
        "flags": "0",
        "ip_address": "10.18.1.121",
        "source": "Other",
        "isoverdue": "0",
        "isanswered": "1",
        "duedate": null,
        "reopened": "2015-04-20 07:25:42",
        "closed": "2015-04-20 07:25:54",
        "lastmessage": "2015-04-20 07:18:33",
        "lastresponse": "2015-04-20 07:25:54",
        "created": "2015-04-20 07:18:33",
        "updated": "2015-04-20 07:25:54"
    },

您必须将 JSON 编码放在 while 循环之外:

while($row = $result->fetch_assoc()) {
    $rows[] = $row;
}

//Add the header...
header('Content-Type: application/json');
echo json_encode($rows);

否则,您只是连接多个 JSON 数组,这会导致整体 JSON 无效。

您正在循环中进行编码。它应该在循环之后完成:

while(...) { 
   build array
}
echo json_encode($array);

就目前而言,您正在输出多个单独的 json 字符串,例如

{"foo":"bar"}{"baz":"qux"}

这在语法上是非法的 JSON。如果你在循环之后进行编码,你会得到

[{"foo":"bar"},{"baz":"qux"}]

这是合法的。

最重要的是,您会在每个循环迭代中输出 header(),导致大量 "headers already sent" 警告消息,这进一步加剧了损坏。

json 响应必须包含一个语法上有效的 json 字符串,除此之外别无其他。