我正在研究酒店API,它需要以JSON形式发送数据,
但我的JSON表单错误,API无效。
我的代码是: -
$json = json_encode([
'RoomGuests' => [ json_encode(["NoOfAdults"=> 1,"NoOfChild"=> 0,"ChildAge"=> null])],
]);
我的输出是: -
{
"RoomGuests": [
"{\"NoOfAdults\":1,\"NoOfChild\":0,\"ChildAge\":null}"
] }
我希望结果如下: -
{
"RoomGuests": [{
"NoOfAdults": 1,
"NoOfChild": 0,
"ChildAge": null
}], }
请让我知道如何解决这个问题。
答案 0 :(得分:2)
使用此
$json = json_encode([
'RoomGuests' => [["NoOfAdults"=> 1,"NoOfChild"=> 0,"ChildAge"=> null]],
]);
echo $json;
这将导致
{
"RoomGuests": [
{
"NoOfAdults": 1,
"NoOfChild": 0,
"ChildAge": null
}
]
}
答案 1 :(得分:1)
正确输出而不是:
$json = json_encode([
'RoomGuests' => [ json_encode(["NoOfAdults"=> 1,"NoOfChild"=> 0,"ChildAge"=> null])],
]);
足以让:
$json = json_encode([
'RoomGuests' => [ ["NoOfAdults"=> 1,"NoOfChild"=> 0,"ChildAge"=> null] ],
]);
json_encode
将递归编码所有子数组。所以只召唤一次就足够了。
答案 2 :(得分:0)
这是我的代码,以json格式打印输出。您可以按照您的编辑方式进行编辑:
<?php
include_once('connect.php');
{
error_reporting( error_reporting() & ~E_NOTICE );
$id = $_GET['id'];
$name = $_GET['name'];
$date = $_GET['date'];
$select = "select * from details";
$sel = "select id,name from details";
$res = mysqli_query($con, $select);
$result = mysqli_query($con,$sel);
while($row = mysqli_fetch_object($res))
{
$output[] = $row;
}
if(empty($output))
{
print(json_encode("User doesn't exist"));
}
else{
while($col = mysqli_fetch_object($result))
{
$output[] = $col;
}
if(empty($output))
{
print(json_encode("User doesn't exits"));
}
else{
print(json_encode($output));
}
}
}
?>
答案 3 :(得分:0)
1142 : INSERT command denied to user 'readonly'@'localhost' for table 'TempPermissionsTest'
结果:
<?php
$a = (object)[
'RoomGuests' => [
(object)["NoOfAdults"=> 1,"NoOfChild"=> 0,"ChildAge"=> null]
],
];
var_export(json_encode($a));