php 解析HTML post過來的json字串

來源:互聯網
上載者:User
我在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));//好吧,結果還是空的




回複討論(解決方案)

也真難為你了,做那麼複雜的編碼處理

$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] =>
)

)

也真難為你了,做那麼複雜的編碼處理

$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] =>         ))



還是不行哦
但是我在php裡直接寫$json_string='[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';是可以的,難道POST裡的資料不一樣?

$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] =>
)

)


這個方法試過了,並沒有錯,只是如果把
$s = '[{\\"table\\":\\"a\\",\\"field\\":\\"value\\",\\"max\\":60,\\"min\\":null}]';

換成
$s=$_POST['json'];

就不行了
可以問題出在POST的資料上,
但是在HTML裡用js的eval把字串轉為json對象又能正常換行哦

echo base64_encode($_POST['json']);
貼出結果

base64_encode後echo出來

W3tcXCZhbXA7cXVvdDt0YWJsZVxcJmFtcDtxdW90OzpcXCZhbXA7cXVvdDtlcHFcXCZhbXA7cXVvdDssXFwmYW1wO3F1b3Q7ZmllbGRcXCZhbXA7cXVvdDs6XFwmYW1wO3F1b3Q7c3RhbmQ0XFwmYW1wO3F1b3Q7LFxcJmFtcDtxdW90O21heFxcJmFtcDtxdW90Ozo2MCxcXCZhbXA7cXVvdDttaW5cXCZhbXA7cXVvdDs6bnVsbH1dW10=W10=

echo base64_encode($_POST['json']);
貼出結果


看了base64的編碼和解碼知道了,要用兩次html_entity_decode才行,謝謝版主大人,謝謝jam00,結賬

HTML裡看到的是

 

但是到了PHP就成了
[{\\"table\\":\\"epq\\",\\"field\\":\\"stand4\\",\\"max\\":60,\\"min\\":null}]

(echo輸出到html是看不到"這個的)
所以要html_entity_decode兩次

$s = 'W3tcXCZhbXA7cXVvdDt0YWJsZVxcJmFtcDtxdW90OzpcXCZhbXA7cXVvdDtlcHFcXCZhbXA7cXVvdDssXFwmYW1wO3F1b3Q7ZmllbGRcXCZhbXA7cXVvdDs6XFwmYW1wO3F1b3Q7c3RhbmQ0XFwmYW1wO3F1b3Q7LFxcJmFtcDtxdW90O21heFxcJmFtcDtxdW90Ozo2MCxcXCZhbXA7cXVvdDttaW5cXCZhbXA7cXVvdDs6bnVsbH1dW10=W10=';$s = base64_decode($s);$s = str_replace('\\', '', $s);$s = html_entity_decode($s);$s = html_entity_decode($s);echo $s, PHP_EOL;print_r(json_decode(substr($s, 0, -4), 1));
[{"table":"epq","field":"stand4","max":60,"min":null}][]?Array(    [0] => Array        (            [table] => epq            [field] => stand4            [max] => 60            [min] =>         ))
自己看看就知道是怎麼回事了
其實這種一般性調試,是應該熟練掌握的

自己看看就知道是怎麼回事了
其實這種一般性調試,是應該熟練掌握的


謝謝版主!
我用PHP沒多久,也沒系統的學過,很多都還不會
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.