获取无效的JSON响应

时间:2018-08-11 09:51:49

标签: php rest web-services post

嗨,我正尝试使用rest服务获取时间表信息,但它在以string(562)开头的开头提供了无效的JSON字符。我正在尝试使用提供的此服务以在android应用程序中使用。

我的服务如下:

    <?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"
        }
    ]
}"

关于如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:1)

您的输出是因为使用var_dump(),您应该echo取而代之...

echo json_encode($out,JSON_PRETTY_PRINT);

var_dump()对于显示变量的作用远胜于手册中的值。

  

此功能显示有关一个或多个的结构化信息   包含其类型和值的表达式。数组和对象是   递归地探索以缩进的值来显示结构。