如何解码 json 数据数组


How to decode json data array

我的php脚本如下:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "college";
$con = mysqli_connect($host,$user,$pass,$db);
$query = "SELECT firstname, secondname, pic FROM student";
$result = mysqli_query($con,$query);
$response = array();
While($row= mysqli_fetch_array($result))
{
array_push($response,array('fname'=>$row[0], 'sname'=>$row[1], 'pic'=>$row[2]));
}
mysqli_close($con);
echo json_encode(array('server_response'=>$response));
?>

输出:

{"server_response":[{" fname":"john","sname":"mark","pic":"http:'/'/localhost'/ServerSide'/jm.jpg"}]}

如何解码 JSON 数据以获得具有正确 url 的输出,如下所示:

{"server_response":[{" fname":"john","sname":"mark","pic":"http://localhost/ServerSide/jm.jpg"}]}

最好的方法是替换这些字符串:

echo str_replace(''/','/',json_encode(array('response'=>$response)));

或者尝试条形斜杠

另一种选择是使用此参数进行编码,但需要 php 5.4 或更高版本。

echo json_encode(array('response'=>$response),JSON_UNESCAPED_SLASHES);