2009-12-03 77 views
2

是否有任何解決方法this bugjson_encode url失敗

echo json_encode(array('url'=>'/foo/bar')); 
{"url":"\/foo\/bar"} 

我用Zend_Json and Zend_Json_Expr所以我可以讓我的JS對象中甚至回調函數 - 但我不能讓一個URL可用格式出來!

echo Zend_Json::encode(array(
         'url'=>new Zend_Json_Expr('/foo/bar'), 
         ), false, 
         array(
         'enableJsonExprFinder' => true), 
         )); 

生產:

{"url":/foo/bar} 

這顯然是不正確的或者..

反正是有得到:

{"url":"/foo/bar"} 

,而無需做任何事情荒謬喜歡找在將它發送到stdio之前將它重新排列的一種方法?

回答

7

{"url":"\/foo\/bar"}對於「/ foo/bar」實際上是完全有效且正確的JSON。嘗試使用json_decode()Zend_Json::decode()解碼該值,並且它應該正確輸出原始網址。

+0

是的,這是正確的JSON,並進一步調查window.location ='http:\/\/www.google.com \ /';似乎工作..看起來像它是我使用的插件中的錯誤:-p – 2009-12-03 20:31:56

+0

而且,使用_Expr本質上只是告訴編碼器不要將它放在引號中或轉義值 - 就像Zend_Db_Expr。 – Justin 2009-12-04 00:21:39