[英]Fix JSON Format from MySQL/PHP
在下面的两个版本的代码中,第一个产生了结果,但请注意“text”数组的末尾没有结束括号。 来自其他代码示例 SEEMS 的第二个输出应该可以工作,但它完全失败了。 我哪里做错了?
当我这样做时;
foreach($db_found->query($sql) as $row) {
$json_array[] =
array('start_date' =>
array('minute' => $row[min], 'hour' => $row[hour],
'month' => $row[mo], 'day' => $row[day], 'year' => $row[yr]),
'text' =>
array('text' => $row[text],
'group' =>
array('group' => $row[callsign])))
;
}
$data = array("events" => $json_array);
echo json_encode($data);
我明白了:
{
"events":[
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"BILL SWEENEY Opened the net from 65.255.143.178 on 146.655MHz, PL94.8Hz",
"group":{
"group":"W0WTS"
}
}
},
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"Peculiar: Clear, 19.9F, wind: N @ 15, humidity: 54%",
"group":{
"group":"GENCOMM"
}
}
}
]
}
但我需要的是这个:
{
"events":[
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"BILL SWEENEY Opened the net from 65.255.143.178 on 146.655MHz, PL94.8Hz"
},
"group":{
"group":"W0WTS"
}
},
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"Peculiar: Clear, 19.9F, wind: N @ 15, humidity: 54%"
},
"group":{
"group":"GENCOMM"
}
}
]
}
我也试过:
foreach($db_found->query($sql) as $row) {
$json_array[] =
array('start_date' =>
array('minute' => $row[min], 'hour' => $row[hour],
'month' => $row[mo], 'day' => $row[day], 'year' => $row[yr]),
array('text' =>
array('text' => $row[text]),
array('group' =>
array('group' => $row[callsign]))
;
}
$data = array("events" => $json_array);
echo json_encode($data);
但这根本行不通。 我究竟做错了什么。
因此,我开始美化您的 JSON 和 PHP,然后重新格式化您的代码,以便更容易理解所需的数据层次结构以及编码数据层次结构。 这是重新格式化:
<?php
foreach ($db_found->query($sql) as $row) {
$json_array[] =
[
'start_date' =>
[
'minute' => $row['min'],
'hour' => $row['hour'],
'month' => $row['mo'],
'day' => $row['day'],
'year' => $row['yr']
],
'text' =>
[
'text' => $row['text'],
'group' =>
[
'group' => $row['callsign']
]
]
];
}
$data = ["events" => $json_array];
echo json_encode($data);
?>
您可以看到“group”数组位于“text”数组内。 在重新格式化你想要的 JSON 之后,我认为这就是你要找的:
以及产生我认为您正在寻找的输出的代码:
<?php
foreach ($db_found->query($sql) as $row) {
$data["events"][] =
[
'start_date' =>
[
'minute' => $row['min'],
'hour' => $row['hour'],
'month' => $row['mo'],
'day' => $row['day'],
'year' => $row['yr']
],
'text' =>
[
'text' => $row['text']
],
'group' =>
[
'group' => $row['callsign']
]
];
}
echo json_encode($data);
?>
笔记:
我选择这种特殊格式是因为它是理解层次结构的最简单方法。 通常我不会像这样将[
放在它自己的行上,但在这种情况下它更容易处理。
我选择[]
作为数组定义而不是array()
因为它的视觉噪音要少得多,因此更容易理解
我在循环中使用了$data["events"][] =
,因为我认为它可以更清楚地说明您定义的数据。 或者,我可能会执行$events[] =
或$eventsJson[] =
以便变量明确它应该保存什么信息,然后执行$data=['events'=>$eventsJson];
$row[string]
不向前兼容。 见https://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar
试试这个代码块
$json_array[] =
array(
'start_date' =>
array(
'minute' => $row[min],
'hour' => $row[hour],
'month' => $row[mo],
'day' => $row[day],
'year' => $row[yr]
),
'text' =>
array('text' => $row[text]), // this is the change
'group' =>
array('group' => $row[callsign]) // so here matching bracket must be maintain
);
OUTP 结构是您想要的带有空数据的结构,因为我没有循环数据
{
"events": [
{
"start_date": {
"minute": null,
"hour": null,
"month": null,
"day": null,
"year": null
},
"text": {
"text": null
},
"group": {
"group": null
}
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.