Home >Backend Development >PHP Tutorial >php 解析HTML post过来的json字符串,该怎么解决

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

WBOY
WBOYOriginal
2016-06-13 12:01:331310browse

php 解析HTML post过来的json字符串

本帖最后由 asia_deng 于 2014-07-07 15:48:48 编辑 我在js里把一个json对象转为json字符串,然后放到一个隐含的input里提交到php
这是HTML的部分
<br /><br /><input type="hidden" name="epqsql" id="epqsql" value="[{"table":"epq","field":"stand4","max":60,"min":null}]"><br />

php里获取到的字符串是:

[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]<br />

对字符串处理
<br />$json_string=$_POST['json'];<br />$json=htmlspecialchars_decode($json_string);<br />print_r(json_decode($json));//结果是空的<br />


换一下
<br />$json=stripslashes(htmlspecialchars_decode($json_string));<br />print_r(json_decode($json));//结果还是空的<br />


再改一下
<br />$json=stripslashes(stripslashes(htmlspecialchars_decode($json_string)));<br />print_r(json_decode($json));//好吧,结果还是空的<br />




------解决方案--------------------
本帖最后由 xuzuning 于 2014-07-07 15:57:51 编辑 也真难为你了,做那么复杂的编码处理
$s = '[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';<br /><br />$s = html_entity_decode($s);<br />$s = stripslashes($s);<br /><br />print_r(json_decode($s, 1));<br />
Array<br />(<br />    [0] => Array<br />        (<br />            [table] => a<br />            [field] => value<br />            [max] => 60<br />            [min] => <br />        )<br /><br />)<br /><br />

------解决方案--------------------
$str='[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';<br />$new=htmlspecialchars_decode($str);<br /><br />$new=str_replace('\\','',$new);<br /><br />$new1=json_decode($new,true);<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($new1);<br />echo "
";
Array
(
    [0] => Array
        (
            [table] => a
            [field] => value
            [max] => 60
            [min] => 
        )

)
------解决方案--------------------
echo base64_encode($_POST['json']);
贴出结果
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn