2013-04-11 26 views
0

當我使用URLVariables將動作腳本3中的json數據發送到php時,json字符串發生變化,並且不能在php中用作json。如何防止這種情況發生?或者如何解決它?從PHPPHP中丟失的JSON格式

[{"data1":"value1","data2":"value2",...},{...},...] 

echo $_POST['myObject']:從Flash(發送moethod POST,變量名myObject

跟蹤

[{\"data1\":\"value1\",\"data2\":\"value2\",...},{...},...] 

echo json_decode($_POST['myObject'])從PHP是什麼,當var_dump(json_decode($_POST['myObject'])

NULL 
+3

使用'回聲檢查你的錯誤類型json_last_error();'。 – Rikesh 2013-04-11 08:00:11

回答

4

Th e服務器自動跳過POST數據(正如我記得它是php.ini中的一個選項)。 要取消轉義,使用stripslashes功能,解碼你的字符串後;)

json_decode(stripslashes($_POST['myObject'])); 

基於@therefromhere的意見,較好地解決了設置magic_quotes_gpc關閉。 如果您具有服務器的根訪問權限,或者您有權在運行時設置php標誌,則可以執行此操作。 下面是一些這方面的幫助: http://php.net/manual/en/security.magicquotes.disabling.php

基於@ NL-X的評論,如果你想解決這個問題,從您的服務器配置undepended:

$myObject = get_magic_quotes_gpc() ? //Examine: is magic quotes gpc on? 
       stripslashes($_POST['myObject']) : //if true: unescape the string 
       $_POST['myObject'];    //if false, do nothing 
json_decode($myObject); 
//When php 5.3 or earlier installed on server 
+0

這就是它:)如果這不起作用,你有一個格式不正確的JSON。 – 2013-04-11 08:00:03

+2

更好的是,如果可以的話,設置magic_quotes_gpc = 0,它在5.3中被棄用,並在5.4中被移除(http://php.net/manual/en/function.get-magic-quotes-gpc.php) – 2013-04-11 08:04:01

+0

你救了我的一天! :)刪除與stripslashes斜線做了這份工作! +由於某些類故障,我的確有錯誤的JSON ... 我已經將JSON.stringify更改爲com.adobe.serialization.json.JSON.encode PS:我使用的是PHP 5.2.17服務器 – SzRaPnEL 2013-04-11 08:28:16