当前位置:Gxlcms > PHP教程 > php解析HTMLpost过来的json字符串,该怎么解决

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

时间:2021-07-01 10:21:17 帮助过:19人阅读

php 解析HTML post过来的json字符串
本帖最后由 asia_deng 于 2014-07-07 15:48:48 编辑

我在js里把一个json对象转为json字符串,然后放到一个隐含的input里提交到php
这是HTML的部分




php里获取到的字符串是:

[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":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));//好吧,结果还是空的




------解决方案--------------------
本帖最后由 xuzuning 于 2014-07-07 15:57:51 编辑

也真难为你了,做那么复杂的编码处理
$s = '[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":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='[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';
$new=htmlspecialchars_decode($str);

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

$new1=json_decode($new,true);
echo "
";
print_r($new1);
echo "
";

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

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

人气教程排行