如何解码json数据数组

时间:2016-03-19 19:54:13

标签: php json

我的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"}]}

1 个答案:

答案 0 :(得分:1)

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

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

或者尝试strip slashes

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

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