我已经创建了大部分部分,并且运行良好。但这是唯一嵌套的JSON。如何访问它?我已经尝试了最长的时间,我想我可能会大脑冻结,因为我认为这会很简单。
以下是从JSON文件获取所有保留的代码:
GET - Gets all Reservation by Status
$app->get('/reservation/:status', function($status) {
$dbHandler = new DatabaseHandler();
$dbHandler->connect();
$reservationArray = $dbHandler->runQueryWithOneParam('SELECT * FROM RestaurantDB.Reservation WHERE STATUS = ?', "s", $status);
$response = array();
$response["error"] = false;
$response["reservations"] = array();
while ($reservation = $reservationArray->fetch_assoc()) {
$tmpReservation = array();
$tmpReservation["ID"] = $reservation["ID"];
$tmpReservation["UserName"] = $reservation["UserName"];
$tmpReservation["NoOfPeople"] = $reservation["NoOfPeople"];
$tmpReservation["DateOfReservation"] = $reservation["DateOfReservation"];
$tmpReservation["TimeOfReservation"] = $reservation["TimeOfReservation"];
$tmpReservation["Status"] = $reservation["Status"];
$tmpReservation["TotalAmount"] = $reservation["TotalAmount"];
$tmpReservation["SpecialRequirement"] = $reservation["SpecialRequirement"];
$tmpReservation["Details"] = array();
$reservationDetailArray = $dbHandler->runQueryWithOneParam('SELECT * FROM RestaurantDB.ReservationDetail WHERE ReservationID = ?', "i", $reservation["ID"]);
while ($reservationDetail = $reservationDetailArray->fetch_assoc()) {
$tmpReservationDetails = array();
$tmpReservationDetails["ItemID"] = $reservationDetail["ItemID"];
$tmpReservationDetails["Quantity"] = $reservationDetail["Quantity"];
$tmpReservationDetails["SubTotal"] = $reservationDetail["SubTotal"];
array_push($tmpReservation["Details"], $tmpReservationDetails);
}
array_push($response["reservations"], $tmpReservation);
}
$dbHandler->disconnect();
echoResponse(200, $response);
});
JSON显示为..
{
"ID": 5,
"UserName": "Coca Cola",
"NoOfPeople": 2,
"DateOfReservation": "1.79",
"TimOfReservation": null,
"Status": null,
"TotalAmount": null,
"SpecialRequirement": 0,
"Details": [
{
"ID": 1,
"ItemID": 3,
"Quantity": 2,
"SubTotal": ""
},
{
"ID": 2,
"ItemID": 4,
"Quantity": 2,
"SubTotal": 1.2
}
]
}
例如,我可以使用$reservation ["Status"]
显示预订状态,但我似乎不知道如何从中获取任何字段
"Details": [
{
"ID": 1,
"ItemID": 3,
"Quantity": 2,
"SubTotal": ""
},
以下是PHP代码。。
<?php
include ('libs'WsURLs.php');
include ('libs'WsConsumer.php');
$wsConsumer = new WsConsumer ();
$_POST = array ();
$result = $wsConsumer->executeGET ( GET_RESERVATION );
$reservations = $result ["reservations"];
// Populating the table
foreach ( $reservations as $reservation) {
echo ('<tr>');
echo ('<td>' . $reservation ["Status"] . '</td>');
echo ('</tr>');
?>
$reservation["Details"]
是一个数组,您可以访问数组元素:
$reservation["Details"][$i]["ItemID"]
是预订中第i个项目的项目ID。
您可以循环浏览预订详细信息
foreach($reservation ["Details"] as $key=>value)
参见此示例,您可以遍历数组并提取值:
<?php
$json = '{
"ID": 5,
"UserName": "Coca Cola",
"NoOfPeople": 2,
"DateOfReservation": "1.79",
"TimOfReservation": null,
"Status": null,
"TotalAmount": null,
"SpecialRequirement": 0,
"Details": [
{
"ID": 1,
"ItemID": 3,
"Quantity": 2,
"SubTotal": ""
},
{
"ID": 2,
"ItemID": 4,
"Quantity": 2,
"SubTotal": 1.2
}
]
}';
$data = json_decode($json, true);
for($i = 0; $i < count($data['Details']); $i++) {
echo '<p>';
foreach($data['Details'][$i] as $key => $value) {
echo $key . ' -> ' . $value . '<br>';
}
echo '</p>';
}
在你的情况下,你可以这样做:
$id_search = 2;
foreach ( $reservations as $reservation) {
for($i = 0; $i < count($reservation['Details']); $i++) {
if($id_search == $reservation['Details'][$i]['ID']) {
echo '<p>';
foreach($reservation['Details'][$i] as $key => $value) {
echo $key . ' -> ' . $value . '<br>';
}
echo '</p>';
}
}
}