2013-10-23 370 views
0

我提取JSON值值,但總是收到一個空的結果,當我回顯值獲取從JSON字符串

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]'; 

$obj2 = json_decode($json, true); 

$displayName = $obj2->Surname; 

echo"$displayName"; 
+1

那是什麼'$ xmlresponse'? –

+1

$ xmlresponse應該是$ json嗎?美元價值從何而來? – Jompper

+0

這需要進行基本的調試。當你做一個'print_r($ obj2);'時,你會得到什麼? –

回答

0

試試這個:

<?php 
$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]'; 
$obj2 = json_decode($json, true); 
$displayName = $obj2[0][0]['Surname']; 
echo "$displayName"; 
?> 
1

你必須在其他一個一個對象在這個JSON字符串

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]'; 

$obj2 = json_decode($json); 

print_r($obj2); 
0

這應該是這樣的:

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]'; 

$obj2 = json_decode($json, true); 

$displayName = $obj2->Surname; 

echo"$displayName"; 

你混淆/彌補變量名...

0

有你所提供的代碼的幾個問題。 首先,您的json文本存儲在$json中,但您嘗試解碼$xmlresponse。我想這只是一個複製/粘貼錯誤,但。 其次,儘管您明確強制json_decode將對象解碼爲關聯數組,但您仍嘗試使用對象語法訪問姓氏。第三,json提供了一個數組中的一個對象的數組。您忽略響應的嵌套結構。

試試這個:

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]'; 
$response = json_decode($json); 
$displayName = $response[0][0]->Surname; 

echo $displayName;