2011-10-06 61 views
0

我有JSON字符串,如:json_decode php5.3錯誤或錯過了解?

'"[{\"type\": \"EDITOR\", \"value\": \"fsddfsdsfdfs\"}, {\"type\": \"CITA\", \"value\": \"Bug:\\n\\t\\t\\t\\t\\t0 open/0\\n\\t\\t\\t\\n \\n Feature:\\n\\t\\t\\t\\t\\t1 open/1\\n\\t\\t\\t\"}]"' 

json_decode未能解碼。

刪除「從開始和結束標記使得它的工作

'[{\"type\": \"EDITOR\", \"value\": \"fsddfsdsfdfs\"}, {\"type\": \"CITA\", \"value\": \"Bug:\\n\\t\\t\\t\\t\\t0 open/0\\n\\t\\t\\t\\n \\n Feature:\\n\\t\\t\\t\\t\\t1 open/1\\n\\t\\t\\t\"}]' 

測試代碼

$test1 = '"[{"type": "EDITOR", "value": "fsddfsdsfdfs"}, {"type": "CITA", "value": "Bug: 0 open/0 Feature: 1 open/1 "}]"'; 
print_r(json_decode($test1)); 
echo json_last_error().'<br>'; 
$test2 = '[{"type": "EDITOR", "value": "fsddfsdsfdfs"}, {"type": "CITA", "value": "Bug: 0 open/0 Feature: 1 open/1 "}]'; 
print_r(json_decode($test2)); 

我缺少的東西或用錯了?

+4

您似乎已經有了解決方案:刪除開頭和結尾''' –

+0

所有這些反斜槓都是字符串的一部分?!? – deceze

回答

2

您不應該將您的字符串稱爲「JSON字符串」,因爲它不是。你當然沒有使用json_encode()得到這個字符串。該grammar for JSON只有兩個頂級製作:

JSON is either an object {...} or an array [...] (and nothing else) 

由此得出結論:

JSON-string is either "{...}" or "[...]" or '{...}' or '[...]' (and nothing else) 
+0

我沒有使用json_encode()我在JavaScript中使用了JSON.stringify數組。之後,我將它發送到服務器。 – user257980

+0

因此,您可能會將附加引號添加到JSON.stringify()獲得的字符串中。發送前可能發生在JavaScript中,或者在收到後發生在PHP中。 – Jiri

2

」標記在開始並結束將其內容標記爲字符串,禁用解析器以檢測對象({})和數組([])

刪除它,因爲你發現它會工作。

0

這appened把我從PHP5.2遷移到5.3之後了。

我有一個腳本,使用JSON.stringify(響應)從JavaScript發送並在PHP中使用json_decode接收,並停止工作。

不知道從兩個PHP版本的差異,但我不得不添加stripslashes,使我再次工作。

$ array = json_decode(stripslashes($ IDS));