JSON格式失去了在PHP失去了、格式、JSON、PHP

2023-09-08 15:05:50 作者:你若离去我便死去

当我发送JSON数据从动作脚本3到PHP使用的URLVariables ,JSON字符串的变化,不能作为JSON里面的PHP。如何prevent这种情况发生?或如何解决它?

从Flash跟踪(发送moethod POST ,变量名 myObject的):

  [{DATA1:值1,数据2:值2,...},{...},...]
 

回声$ _ POST ['myObject的'] 从PHP:

  [{\数据1 \:\值1 \,\数据2 \:\值2 \,...},{...} ,. ..]
 
php操作json文件

回声json_de code($ _ POST ['myObject的'])从PHP是什么,当的var_dump(json_de code ($ _ POST ['myObject的'])

  NULL
 

解决方案

,服务器会自动躲避 POST 数据(我记得是在的php.ini )。 要反向转义,使用函数stripslashes 功能,以后去code的字符串;)

  json_de code(函数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()? //检查:是魔术引号GPC吗?
               函数stripslashes($ _ POST ['myObject的'])://如果为true:反向转义的字符串
               $ _ POST ['myObject的']; //如果假的,什么都不做
json_de code($ myObject的);
//当PHP 5.3或更早版本的服务器
 

When I send json data from action script 3 to php using URLVariables, the json string changes and cannot be used as json inside php. How to prevent this happening? Or how to fix it?

trace from Flash(send moethod POST, variable name myObject):

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

echo $_POST['myObject'] from PHP:

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

echo json_decode($_POST['myObject']) from PHP is nothing, when var_dump(json_decode($_POST['myObject']):

NULL

解决方案

The server automatically escape the POST data (As I remember it is an option in php.ini). To unescape , use stripslashes function, and after decode your string ;)

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

Based on @therefromhere 's comment, a better solution to set magic_quotes_gpc off. You can do this if you have a root access for the server, or you have permission to set php flags at runtime. Here is some help for this: http://php.net/manual/en/security.magicquotes.disabling.php

Based on @nl-x 's comment if you want to solve this problem, undepended from your server configuration:

$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