[英]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.