我一直在我的网页服务中使用引号接收我的JSON,如下所示:
"[{"id":"1","nombre":"Cecilia","correo":"csoto@ts.edu.mx","telefono":"5529964"},{"id":"2","nombre":"Romero","correo":"","telefono":"5435432"},{"id":"3","nombre":"nick","correo":"carlos@mail.com","telefono":"12345"}]"
注意括号前的“”,我不知道为什么。 我正在使用jacwright的restServer(https://github.com/jacwright/RestServer)来完成这个功能:
public function getUsers()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "estudiantes";
// Create connection
$array = array();
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, nombre, correo, telefono FROM estudiante";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$res = array("id" => $row["id"], "nombre" => $row["nombre"], "telefono" => $row["telefono"], "correo" => $row["correo"]);
array_push($array, $res);
//echo "id: " . $row["id"]. " - Name: " . $row["nombre"]. " " . $row["correo"]. "<br>";
}
}
$conn->close();
return json_encode($array) ;
}
有人能够了解为什么会不断添加这些引号吗?
答案 0 :(得分:1)
只是一个模拟案例的快速示例,使用echo返回JSON编码数组:
<强> JS:强>
$.get('json.php', function(data) {
console.log(data);
if (data) {
//Check the console, everything is working like charm
newData = $.parseJSON(data);
console.log(data);
console.log('--------------' + 'Elements count:' + newData.length);
console.log('--------------' + newData);
}
});
<强> json.php:强>
<?php
$myArr = array(
array("id"=>"1","nombre"=>"Cecilia","correo"=>"csoto@ts.edu.mx","telefono"=>"5529964"),
array("id"=>"2","nombre"=>"Romero","correo"=>"","telefono"=>"5435432"),
array("id"=>"3","nombre"=>"nick","correo"=>"carlos@mail.com","telefono"=>"12345")
);
$jsonData = json_encode($myArr);
echo $jsonData;
?>
在 json.php 中使用return $jsonData;
代替echo $jsonData;
的上述代码相同,它在控制台中提供了以下内容:
data
ReferenceError:未定义newData
答案 1 :(得分:0)
在返回的字符串中获取引号的原因是服务器(https://github.com/jacwright/RestServer)正在执行json_encode。因此,当您从函数getUsers
返回数据并且已使用json_encode
时,它将转换为json字符串,当RestServer将其返回给您时,它仅将其视为字符串,因此在其周围添加引号。
如果查看TestController
,您会看到它返回一个对象$user
而不是json_encode($user)
,而其中的sendData
函数中的RestServer实际上使用了json_encode
功能
所以你的代码必须是:
public function getUsers()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "estudiantes";
// Create connection
$array = array();
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, nombre, correo, telefono FROM estudiante";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$res = array("id" => $row["id"], "nombre" => $row["nombre"], "telefono" => $row["telefono"], "correo" => $row["correo"]);
array_push($array, $res);
//echo "id: " . $row["id"]. " - Name: " . $row["nombre"]. " " . $row["correo"]. "<br>";
}
}
$conn->close();
return $array;
}
另请参阅第414行https://github.com/jacwright/RestServer/blob/master/source/Jacwright/RestServer/RestServer.php#L414中的sendData
功能。