2011-01-22 79 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对象。