gpt4 book ai didi

php - JSON 编码语法不正确

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:19 24 4
gpt4 key购买 nike

我按照问题 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"
},

最佳答案

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

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

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

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

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

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

这是合法的。

最重要的是,您将在每个循环迭代中输出您的 header(),导致大量“ header 已发送”警告消息,这进一步加剧了损坏。

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

关于php - JSON 编码语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29756204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com