简体   繁体   English

防止json_encode添加转义字符

[英]prevent json_encode adding escape characters

Is there a solution to prevent json_encode adding escape characters? 有没有一个解决方案来防止json_encode添加转义字符? I am returning a json obj from an ajax request. 我从ajax请求返回一个json obj。

Here is what I currently have: 这是我目前拥有的:

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 );

which delivers: 它提供:

[
{\"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}
]

but I need: 但是我需要:

[
{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}
]

having the imgurl width and height indexes quoted is causing the rest of my javascript to break 引用imgurl宽度和高度索引导致我的其余javascript中断

not having much luck so any tips very much welcome... 没有太多的运气,所以任何提示非常欢迎...

Using the code you have in your question and var_dumping it, I get the following: string(40) "[{"imgurl":"\\/bla"},{"imgurl":"\\/blub"}]" 使用你在问题中的代码和var_dumping它,我得到以下结果: string(40) "[{"imgurl":"\\/bla"},{"imgurl":"\\/blub"}]"

Only if I double the json_encode like $t = json_encode(json_encode($farr)); 只有当我将$t = json_encode(json_encode($farr)); I get the same result as you - so there must be a second json_encode somewhere… 我得到了与你相同的结果 - 所以必须在某处有第二个json_encode ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM