php 解析HTML post过来的json字符串,该怎么解决

php 解析HTML post过来的json字符串
本帖最后由 asia_deng 于 2014-07-07 15:48:48 编辑
我在js里把一个json对象转为json字符串,然后放到一个隐含的input里提交到php
这是HTML的部分


<input type="hidden" name="epqsql" id="epqsql" value="[{&quot;table&quot;:&quot;epq&quot;,&quot;field&quot;:&quot;stand4&quot;,&quot;max&quot;:60,&quot;min&quot;:null}]">

php里获取到的字符串是:

[{\\&quot;table\\&quot;:\\&quot;a\\&quot;,\\&quot;field\\&quot;:\\&quot;value\\&quot;,\\&quot;max\\&quot;:60,\\&quot;min\\&quot;:null}]

对字符串处理

$json_string=$_POST['json'];
$json=htmlspecialchars_decode($json_string);
print_r(json_decode($json));//结果是空的


换一下

$json=stripslashes(htmlspecialchars_decode($json_string));
print_r(json_decode($json));//结果还是空的


再改一下

$json=stripslashes(stripslashes(htmlspecialchars_decode($json_string)));
print_r(json_decode($json));//好吧,结果还是空的

php 解析HTML post过来的json字符串,该怎么解决


------解决方案--------------------
本帖最后由 xuzuning 于 2014-07-07 15:57:51 编辑
也真难为你了,做那么复杂的编码处理
$s = '[{\\&quot;table\\&quot;:\\&quot;a\\&quot;,\\&quot;field\\&quot;:\\&quot;value\\&quot;,\\&quot;max\\&quot;:60,\\&quot;min\\&quot;:null}]';

$s = html_entity_decode($s);
$s = stripslashes($s);

print_r(json_decode($s, 1));
Array
(
    [0] => Array
        (
            [table] => a
            [field] => value
            [max] => 60
            [min] => 
        )

)


------解决方案--------------------
$str='[{\\&quot;table\\&quot;:\\&quot;a\\&quot;,\\&quot;field\\&quot;:\\&quot;value\\&quot;,\\&quot;max\\&quot;:60,\\&quot;min\\&quot;:null}]';
$new=htmlspecialchars_decode($str);

$new=str_replace('\\','',$new);

$new1=json_decode($new,true);
echo "<pre>";
print_r($new1);
echo "</pre>";

Array
(
    [0] => Array
        (
            [table] => a
            [field] => value
            [max] => 60
            [min] => 
        )

)
------解决方案--------------------
echo base64_encode($_POST['json']);
贴出结果