Php 获取意外错误';}';在postman中打印json数据时
我已经为一个移动应用程序服务编写了一些代码,我需要以json格式输出,当我在postman上测试时,我得到了这个错误:意外的'}',但是当我以数组的形式打印它时,它工作得很好,但在json格式中我遇到了一个问题,我不明白为什么它会显示错误,我觉得它很好,请检查下面的代码 多谢各位Php 获取意外错误';}';在postman中打印json数据时,php,arrays,json,Php,Arrays,Json,我已经为一个移动应用程序服务编写了一些代码,我需要以json格式输出,当我在postman上测试时,我得到了这个错误:意外的'}',但是当我以数组的形式打印它时,它工作得很好,但在json格式中我遇到了一个问题,我不明白为什么它会显示错误,我觉得它很好,请检查下面的代码 多谢各位 <?php //$dbcon=mysqli_connect("localhost","root","","testing"); $username = $_REQUEST['username']; if($us
<?php
//$dbcon=mysqli_connect("localhost","root","","testing");
$username = $_REQUEST['username'];
if($username != '') {
$sql= "select * from test j,testing_user u where j.employer_id=u.id order by `date_added` desc limit 10";
$query= mysqli_query($dbcon,$sql);
$records = array();
$rows = array();
if(mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_object($query)) {
$rows['job_id'] = $row->job_id;
$rows['job_title'] = addslashes($row->job_title);
$rows['username'] = $row->username;
$rows['userid'] = $row->id;
$rows['email'] = $row->email;
$sql1 = mysqli_query($dbcon,"select * from test_company where company_id=".$row->company);
$row1 = mysqli_fetch_object($sql1);
$rows['companyname'] = $row1->name;
$rows['reccruitername'] = $row1->contact_name;
$rows['duration'] = $row->duration;
$skills = trim(addslashes(strip_tags($row->skills_prefered))).PHP_EOL;
$jobskills = utf8_encode($skills);
$rows['job_skills'] = str_replace(' ',' ',$jobskills);
$description = trim(addslashes(strip_tags($row->description))).PHP_EOL;
$desc = utf8_encode($description);
$rows['job_description'] = str_replace(' ',' ',$desc);
$sql2 = mysqli_query($dbcon,"select * from test_cities where code='".(int)$row->city. "'");
$crow = mysqli_fetch_object($sql2);
$sql3 = mysqli_query($dbcon,"select * from test_states where code='".$row->state."'");
$srow = mysqli_fetch_object($sql3);
$rows['joblocation'] = $crow->name.','.$srow->name;
$catsql = mysqli_query($dbcon,"SELECT name FROM test_business WHERE id = '" . (int)$row->category . "'");
$catrow = mysqli_fetch_object($catsql);
$exp = unserialize($row->experience);
$expsql = mysqli_query($dbcon,"SELECT exp_value FROM test_experience WHERE id = '" . (int)$exp['to'] . "'");
$experience = mysqli_fetch_object($expsql);
$rows['jobcategory'] = $catrow->name;
$rows['experience'] = $experience->exp_value;
$rows['zipcode'] = $row->zipcode;
$rows['areacode'] = $row->areacode;
if (preg_match('/[^A-Za-z0-9]+/', $row->job_type)) {
$rows['job_type'] = $row->job_type;
} else {
$params = unserialize($row->job_type);
//implode into a string
$param = implode(",",(array)$params);
$typequery = mysqli_query($dbcon,"SELECT * FROM test_type WHERE id IN ($param)");
$types = mysqli_fetch_object($typequery);
$rows['job_type'] = $types->job_type;
}
$applysql = mysqli_query($dbcon,"select * from testapp_jobs where job_id=".$row->job_id." and jobseeker_id=".$row->id);
$applyrow = mysqli_fetch_object($applysql);
if(!empty($applyrow)) {
$rows['job_status'] = '1';
} else {
$rows['job_status'] = '0';
}
$str = 'No of Vacancies: '.$row->num_jobs.PHP_EOL.'Hourly Pay:'.$row->hourly_pay.PHP_EOL.'Duration:'.$row->duration;
$rows['job_extrainfo'] = utf8_encode($str);
$rows['posted'] = $row->publish_on;
$records[] = $rows;
//print_r($records); die;
}
header('Content-Type: application/json');
echo '{"status":"1","message":"Jobs list found.","jobslist":'.json_encode($records).'}';
} else {
header('Content-Type: application/json');
echo '{"status":"0","message":"No Jobs found."}';
}
} else {
header('Content-Type: application/json');
echo '{"status":"0","message":"Required Fields Missing."}';
}
手动格式化Json可能会导致混乱,并且容易出错 更好的解决方法是使用 以下是您应该更改的行:
//...
echo json_encode(array('status' => '1', 'message' => 'job list found', 'joblist' => $records));
//...
echo json_encode(array('status' => 0, 'message' => 'No jobs found'));
//...
echo json_encode(array('status' => 0, 'message' => 'Required Fields Missing.'));
我建议您使用
json\u encode()
构建所有输出,而不仅仅是位。手动执行此操作可能会导致一些问题,如您遇到的问题。不要尝试手动格式化json,因为它很容易出错,请尝试echo json_encode(['status'=>'1','message'=>'job list found','job list'=>$records])
和echo json_encode(['status'=>0,'message'=>'no jobs found'])代码>,最后是echo json_encode(['status'=>0,'message'=>'必填字段缺失])代码>谢谢你的建议xanadev