php 解析HTML post过来的json字符串,该怎么解决
php 解析HTML post过来的json字符串
我在js里把一个json对象转为json字符串,然后放到一个隐含的input里提交到php
这是HTML的部分
php里获取到的字符串是:
对字符串处理
换一下
再改一下
------解决方案--------------------
也真难为你了,做那么复杂的编码处理
------解决方案--------------------
Array
(
[0] => Array
(
[table] => a
[field] => value
[max] => 60
[min] =>
)
)
------解决方案--------------------
echo base64_encode($_POST['json']);
贴出结果
我在js里把一个json对象转为json字符串,然后放到一个隐含的input里提交到php
这是HTML的部分
<input type="hidden" name="epqsql" id="epqsql" value="[{"table":"epq","field":"stand4","max":60,"min":null}]">
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));//好吧,结果还是空的
------解决方案--------------------
也真难为你了,做那么复杂的编码处理
$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 "<pre>";
print_r($new1);
echo "</pre>";
Array
(
[0] => Array
(
[table] => a
[field] => value
[max] => 60
[min] =>
)
)
------解决方案--------------------
echo base64_encode($_POST['json']);
贴出结果