我在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沒多久,也沒系統的學過,很多都還不會