我按照问题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"
},
答案 0 :(得分:3)
你正在循环中进行编码。它应该在循环之后完成:
while(...) {
build array
}
echo json_encode($array);
现在,您正在输出多个单独的json字符串,例如
{"foo":"bar"}{"baz":"qux"}
这是语法上非法的JSON。如果你在循环之后进行编码,你就会得到
[{"foo":"bar"},{"baz":"qux"}]
这是合法的。
最重要的是,你会在每次循环迭代时输出header()
,导致大量“已发送的标题”警告消息,这进一步增加了损坏。
json响应必须包含一个语法上有效的json字符串,而不包含任何其他内容。
答案 1 :(得分:1)
您必须将JSON编码放在while循环之外:
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
//Add the header...
header('Content-Type: application/json');
echo json_encode($rows);
否则,您只是连接多个JSON数组,这会导致整体JSON无效。