Is there a solution to prevent json_encode adding escape characters? I am returning a json obj from an ajax request.
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
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"}]"Only if I double the json_encode like
$t = json_encode(json_encode($farr));I get the same result as you – so there must be a second json_encode somewhere…