我正在尝试构建一个php脚本,该脚本将返回格式正确的json响应,当前我的json响应将带有反斜杠或引用内部对象。
class myObject
{
public $property1;
public $property2;
public $property3;
public $property4;
}
$MyObjects = array();
$results = DB::table('sometable')->get();
foreach ($results as $result) {
$MyObject = new myObject;
$MyObject->property1 = $result->col_1;
$MyObject->property2 = $result->col_2;
$MyObject->property3 = $result->col_3;
//$MyObjects[] = $MyObject;
array_push($MyObjects, $MyObject);
}
var_dump($MyObjects);
echo json_encode($MyObjects);
答案 0 :(得分:0)
要删除反斜杠,您可以编写-
-replace
答案 1 :(得分:-1)
您必须尝试以下代码。它将返回正确的响应。
<?php
$con=mysqli_connect("localhost","root","","mydb");
$response=array();
$query="select * from studmaster";
$result=$con->query($query);
if($result->num_rows>0)
{
$response["student"]=array();
while($rows=$result->fetch_array(MYSQLI_BOTH))
{
$student=array();
$student["studid"]=$rows["studid"];
$student["studname"]=$rows["studname"];
$student["studmobno"]=$rows["studmobno"];
array_push($response ["student"],$student);
}
$response["status"]=1;
$response["message"]="Data Exist";
}
else
{
$response["status"]=0;
$response["message"]="Data Does Not Exist";
}
echo json_encode($response);
?>