2011-01-22 49 views
0

這裏是我的PHP代碼後JSON後如何獲得數據

$response = array(); 

$user1 = array(); 
$user1['name'] = 'Tom'; 
$user1['age'] = '13'; 

$user2 = array(); 
$user2['name'] = 'Jack'; 
$user2['age'] = '20'; 

$response[] = $user1; 
$response[] = $user2; 

echo json_encode($response); 

這裏是我使用jQuery來得到PHP文件數據,如

$.post('file.php', {userid : '1234'}, function(data){ 
    alert(data) //problem here 
}, 'json'); 

問題是如何.html文件在該代碼中使用$ .post後獲取user1的名稱。

謝謝:)

回答

1

因爲你的用戶數組是關聯的,它們被翻譯成JSON對象,而不是數組。

訪問他們data[0].name

0

我想你需要JSON.parse

VAR響應= JSON.parse(數據)

現在的迴應是,擁有所有數據的JSON對象。