2012-08-08 23 views
0

我有從java類返回的以下json。我可以得到這個與JS一起工作。但是,當在php中使用json解碼時,它顯然會中斷,因爲我確信我需要使用regexp來正確解析它。有任何想法嗎?用於java的PHP json解碼返回的轉義json

{"return":"{\"response\": {\n \"header\": {\"status\": \"SUCCESS\"},\n \"table\": {\"rows\": {\"row\": {\"category\": [ {\n \"id\": 1,\n \"name\": \"myApp\",\n \"fa\": [\n {\n \"id\": \"370\",\n \"FieldsAllowed\": \"true\",\n \"systemType\": \"CRM GT\",\n \"cachable\": \"false\",\n \"description\": \"Display Activities\",\n \"faId\": \"100000044\",\n }]}}}\n}}"} 
+0

不,你可能不需要正則表達式。只有一個編碼的字符串,而不是子數組。有人在編碼時犯了最初的錯誤。 – mario 2012-08-08 20:13:04

+0

這是jsonception嗎? – 2012-08-08 20:30:10

+0

不幸的是,這是我堅持,任何想法? – 2012-08-08 20:36:44

回答

0

可以得到數據既可以作爲一個關聯數組或一個對象,

$jsonString = '{"return":"{\"response\": {\n \"header\": {\"status\": \"SUCCESS\"},\n \"table\": {\"rows\": {\"row\": {\"category\": [ {\n \"id\": 1,\n \"name\": \"myApp\",\n \"fa\": [\n {\n \"id\": \"370\",\n \"FieldsAllowed\": \"true\",\n \"systemType\": \"CRM GT\",\n \"cachable\": \"false\",\n \"description\": \"Display Activities\",\n \"faId\": \"100000044\",\n }]}}}\n}}"}'; 

1)獲取響應作爲一個關聯數組,

$decodedResultsAsArray = json_decode($jsonString,true); 
$data = $decodedResultsAsArray['return']; 
print_r($data); 

2)獲取響應作爲對象,

$decodedResultsAsObject = json_decode($jsonString); 
$data = $decodedResultsAsObject->return; 
print_r($data); 

如果你想看看,還有幾個其他選項與json_decode函數,http://php.net/json_decode

0

應用json_decode()兩次得到包含數組(「響應」)。

// This gets you the inner string 
$json = json_decode($input); 

// This unfolds the contained structure 
$data = json_decode($data->return); 

正如你在所有情況下數據編組兩次時所做的一樣。

+0

好的,我嘗試了以下幾種方法,但沒有去?即時通訊仍然重新學習PHP所以我想我不明白如何創建$數據變量? //執行後 $ result = curl_exec($ ch); //關閉連接 curl_close($ ch); //打印($ AgentString); $ json = json_decode($ result); //展開包含的結構 $ data = json_decode($ json-> return); 打印($ data); – 2012-08-08 20:44:17

+0

'print'在陣列上不起作用。 – mario 2012-08-08 20:48:34

+0

好的,所以var_dump還是? – 2012-08-08 20:49:35