我刚刚学习了php
我有一个单独的数据库表,我想将该表与只有一个参数的JSON输出组合在一起,但结果是错误的,如何将我的JSON更改为正确且易于使用的JSON。
感谢
json输出:
[
[
{
"id_service": "3",
"reference_number": "",
"tracking_number": "RJC-0000-0001",
"kd_inbound": "INB-1000-0001",
"tgl_inbound": "2019-11-07 00:00:00",
"status_inb": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbag": "BAG-1468-0002",
"tanggal_outbag": "2019-11-07 00:00:00",
"status_outbag": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbound": "OTB-1826-0001",
"tgl_outbound": "2019-11-07 17:04:49",
"status_otb": "1"
}
],
]
这是我的代码
public function awb_get() {
$id = $this->get('tracking_number');
$res= array(
$this->M_tarif->tampil_status_inbound($id),
$this->M_tarif->tampil_status_otboundbag($id),
$this->M_tarif->tampil_status_otboundori($id),
$this->M_tarif->tampil_status_indes($id),
$this->M_tarif->tampil_status_outdes($id),
$this->M_tarif->tampil_status_runsheet($id),
$this->M_tarif->tampil_db_service_status($id)
);
$this->response($res, 200);
}
我想像这个一样进去
{
"status": 200,
"error": false,
"awb": [
{
"tracking_number": "RJC-0000-0004",
"status": "order",
"tanggal": "2019-10-30"
},
{
"tracking_number": "RJC-0000-0004",
"status": "Inbound to origin",
"tanggal": "2019-11-03"
}
]
}
您的json响应不正确。因此,在发送到函数之前,首先必须重置JSON。我制作了一个样本来重新排列JSON并在stdClass对象中转换它。请检查以下代码以转换您的响应。
$arr ='[
[
{
"id_service": "3",
"reference_number": "",
"tracking_number": "RJC-0000-0001",
"kd_inbound": "INB-1000-0001",
"tgl_inbound": "2019-11-07 00:00:00",
"status_inb": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbag": "BAG-1468-0002",
"tanggal_outbag": "2019-11-07 00:00:00",
"status_outbag": "1"
}
],
[
{
"id_service": "3",
"reference_number": "",
"kd_outbound": "OTB-1826-0001",
"tgl_outbound": "2019-11-07 17:04:49",
"status_otb": "1"
}
],
]';
$result = str_replace(array('[',']','n'), '',htmlspecialchars(json_encode($arr), ENT_NOQUOTES));
$str = preg_replace('/\"/',""", $result);
$json = '[';
$json .= substr($str, 2,-1); // Substring -1 character from the end of the json variable, this will be the trailing comma.
$json .= ']';
$jsonData = preg_replace("/,(?!.*,)/", "", $json);
echo "<pre>";
print_r(json_decode($jsonData));
echo "</pre>";
输出:
Array
(
[0] => stdClass Object
(
[id_service] => 3
[reference_number] =>
[tracking_number] => RJC-0000-0001
[kd_inbound] => INB-1000-0001
[tgl_inbound] => 2019-11-07 00:00:00
[status_inb] => 1
)
[1] => stdClass Object
(
[id_service] => 3
[reference_number] =>
[kd_outbag] => BAG-1468-0002
[tanggal_outbag] => 2019-11-07 00:00:00
[status_outbag] => 1
)
[2] => stdClass Object
(
[id_service] => 3
[reference_number] =>
[kd_outbound] => OTB-1826-0001
[tgl_outbound] => 2019-11-07 17:04:49
[status_otb] => 1
)
)