2016-08-30 28 views
0

我不能讓json_decode()時,該字符串值包含單引號工作(」),如下例所示:PHP json_decode未能當值包含單引號(「)

$result = "{\"message\":\"test \' \",\"report\":[{\"1\":[{\"port\":\"gsm-1.2\",\"phonenumber\":\"XXXXXXXXXXX\",\"time\":\"2016-08-31 00:22:57\",\"result\":\"success\"}]}]}"; 
$resp = json_decode($result, true); 
echo $resp; 
+0

http://stackoverflow.com/questions/8832528/escaping-encoding-single-quotes-in-json-encoded-html5-data-屬性 – Linesofcode

+2

你爲什麼逃脫它?只需使用'''。另外,你不能'回顯'數組。 – AbraCadaver

+1

如果不需要,請不要手動構建JSON字符串。構建正確的對象/數組,然後讓'json_encode()'處理其餘部分。 – Sammitch

回答

0

在代碼中的單引號( ')被斜槓()跳過,因此它打破了JSON格式。

嘗試刪除斜槓並嘗試。它應該工作。

您應該檢查您生成此JSON的代碼。

0

您的$結果json格式不正確,所以我認爲您需要使用stripslashes()來格式化它,並使用json_decode()。它會工作:)。

<?php 
$result = "{\"message\":\"test \'\",\"report\":[{\"1\":[{\"port\":\"gsm-1.2\",\"phonenumber\":\"XXXXXXXXXXX\",\"time\":\"2016-08- 31 00:22:57\",\"result\":\"success\"}]}]}"; 
$result=stripslashes($result); 
$resp = json_decode($result, true); 
var_dump($resp); 
?> 

支票上phpfiddle =>http://phpfiddle.org/main/code/4e7n-vjxa