嗨,我正在尝试使用 rest 服务获取时间表信息,但它在开头给出了一个以 string(562( 开头的无效 JSON 字符。我正在尝试使用此服务以在安卓应用程序中使用。
我的服务如下:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json; charset=utf-8');
include_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));
$result = array();
if(isset($_GET['user_id'])){
$user_id = $_GET['user_id'];
//echo $id;
//SELECT A.subid,A.sub_name,B.class_id,B.class_name,B.class_capacity ,C.ID,C.display_name,D.subject_id ,D.teacher_id,D.route_id,D.start_time,D.end_time,D.end_time,D.weekday FROM wpqm_smgt_time_table D JOIN wpqm_subject A ON D.subject_id =A.subid JOIN wpqm_smgt_class B ON D.class_id=B.class_id JOIN wpqm_users C ON D.teacher_id=C.ID WHERE D.teacher_id=$user_id
$sql = "SELECT B.student_id,B.class_id,A.ID,A.display_name,C.class_id,C.class_name,D.class_id,D.start_time,D.end_time,D.weekday,D.subject_id,E.subid,E.sub_name FROM wpqm_users A JOIN wpqm_marks B ON A.ID=B.student_id JOIN wpqm_smgt_class C ON C.class_id=B.class_id JOIN wpqm_smgt_time_table D ON D.class_id=B.class_id JOIN wpqm_subject E ON D.subject_id=E.subid WHERE ID='$user_id'";
//echo $sql;
$value = mysqli_query($con, $sql);
while($row=mysqli_fetch_array($value))
{
$result[] = array("sub_name" => $row["sub_name"],"start_time" => $row["start_time"],"end_time" => $row["end_time"],"weekday" => $row["weekday"]);
}
}
$in=json_encode($result);
$abc = json_decode($in,true);
$out = [];
foreach($abc as $element) {
$out[$element['weekday']][] = ['sub_name' => $element['sub_name'],'start_time' => $element['start_time'],'end_time' => $element['end_time']];
}
var_dump(json_encode($out,JSON_PRETTY_PRINT));
?>
这是我从服务网址得到的:
string(562) "{
"1": [
{
"sub_name": "English",
"start_time": "3:12:am",
"end_time": "4:11:am"
},
{
"sub_name": "chemistry",
"start_time": "1:0:am",
"end_time": "2:0:am"
}
]
}"
关于如何解决此问题的任何建议?
你的输出是因为你使用了var_dump()
,你应该echo
掉这个值......
echo json_encode($out,JSON_PRETTY_PRINT);
var_dump()
更多的是显示变量而不是值,从手册...
此函数显示有关一个或多个的结构化信息 包含其类型和值的表达式。数组和对象是 使用缩进的值以递归方式探索以显示结构。