有没有防止 json_encode 添加转义字符的解决方案?我正在从 ajax 请求返回一个 json obj。
这是我目前拥有的:
foreach ($files as $file)
{
$inf = getimagesize( $file );
$farr[] = array (
"imgurl" => "/".str_replace( "\\" , "/" , str_replace( DOCROOT , "" , $file ) ) ,
"width" => $inf[0] ,
"height" => $inf[1]
);
}
$t = json_encode( $farr );
Run Code Online (Sandbox Code Playgroud)
它提供:
[
{\"imgurl\":\"\\\/_assets\\\/portfolio\\\/96\\\/full.png\",\"width\":580,\"height\":384},
{\"imgurl\":\"\\\/_assets\\\/portfolio\\\/95\\\/full.png\",\"width\":580,\"height\":452},
{\"imgurl\":\"\\\/_assets\\\/portfolio\\\/94\\\/full.png\",\"width\":580,\"height\":384}
]
Run Code Online (Sandbox Code Playgroud)
但是我需要:
[
{imgurl:"/_assets/portfolio/96/full.png",width:580,height:384},
{imgurl:"/_assets/portfolio/95/full.png",width:580,height:452},
{imgurl:"/_assets/portfolio/94/full.png",width:580,height:384}
]
Run Code Online (Sandbox Code Playgroud)
引用 imgurl 宽度和高度索引会导致我的 javascript 的其余部分中断
运气不好,所以非常欢迎任何提示......
使用您在问题中的代码和 var_dumping 它,我得到以下信息: string(40) "[{"imgurl":"\/bla"},{"imgurl":"\/blub"}]"
只有当我将 json_encode 加倍,就像$t = json_encode(json_encode($farr));我得到和你一样的结果 - 所以一定有第二个 json_encode 某处......