2015-04-23 64 views
1

我做了一个json请求,它工作正常,但它显示了从php文件发送的整个'json数据'。我只需要人的名字。有人可以帮我吗 ?所选字段或参数(名称)的JSON和PHP数据检索

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $('#but1').click(function(){ 
     $.ajax({ 
      url: "http://localhost/task6/callback.php", 
      method: "POST", 
      data:{format:"json"}, 
      type: 'GET', 
      success: function(response) { 
       alert("hi"); 
       var alpha =JSON.parse(response); 
       $('div').html(alpha); 
      } 
     }); 
    }); 
}); 
</script> 
</head> 
<body> 
<button id=but1>Button</button> 
<div> 
Jack 
</div> 
</body> 
</html> 

PHP文件:

<?php 
$arr = '{"List":[{"name": "Maria"},{"name":"Albert"},{"name":"Joseph"}]}'; 
echo json_encode($arr); 
?> 

输出:

{"List":[{"name": "seema"},{"name":"Albert"},{"name":"Sita"}]} 

有人可以帮助我得到的只有这3人的名字,而不是这整个字符串? (我已删除了 “+” 号按评论)

答:@日Thnx Codefox。它的工作之后我所做的更改:

Ajax回调:

success: function(response) { 
alert("hi"); 
var alpha =JSON.parse(response); 
$.each(alpha, function(index,value){ 
$('div').append(alpha[index].name); 
}); 
} 

JSON字符串在PHP中:

$arr = '[{"name": "Maria"},{"name":"Albert"},{"name":"Joseph"}]'; 
+0

使用foreah环.. –

+0

为什么有+号??? –

+0

删除了标志,但仍需要人员的3个名称,而不是整个列表。 – Deadpool

回答

1

首先改变你的代码的PHP文件中删除+形成json字符串。它不应该在那里。

JSON应该是这样的,我认为:

[{"name": "seema"},{"name": "Albert"},{"name": "Sita"}] 

你可以做的第二个变化是:

$('#but1').click(function(){ 
     $.ajax({ 
      url: "http://localhost/task6/callback.php", 
      method: "POST", 
      data:{format:"json"}, 
      dataType: "json", 
      type: 'GET', 
      success: function(response) { 
       alert("hi"); 
       $('div').html(response[0].name); 
      } 
     }); 
    }); 
+0

我做了两件事,但现在屏幕上没有任何东西。 Console.log显示错误“无法读取属性'0'未定义” – Deadpool

+0

编辑帖子,这应该工作。否则设置标题('Content-Type:application/json');在你的php文件中 – CodeFox

+0

是的,它的确如此。我添加了我为使其运行而做出的更改! – Deadpool

0

1 - 我不认为 “+” 是有效的JSON字符串。

2 - 一旦你有了有效的JSON,你可以这样做:

var alpha =JSON.parse(response); 
var names = []; 
alpha.List.forEach(function(element) { 
    names.push(element.name); 
}); 

另一种方式:

var names = alpha.List.map(function(e){ 
    return e.name; 
}); 
0

你需要把 “数据类型” 参数:

$(document).ready(function(){ 
    $('#but1').click(function(){ 
     $.ajax({ 
      url: "http://localhost/task6/callback.php", 
      method: "POST", 
      data:{format:"json"}, 
      dataType:"json", 
      type: 'GET', 
      success: function(response) { 
       console.log('JSON object',response); 
       $('div').html(alpha); 
      } 
     }); 
    }); 
}); 

和..在你的PHP文件..你不需要编码..一个已经被编码的JSON。

只是回应你的JSON。或者,将数组传递给json_encode ..然后,您将拥有一个“新鲜”的JSON。

例子:

<?php 
$myData = ["name"=>"Eduardo","github"=>"http://github.com/eduardostuart"]; 
header('Content-type:application/json'); 
echo json_encode($myData); 
die; 
0

第一解码你的字符串,请检查下面的代码它的工作对我来说..

$arr = '{"List":[{"name": "Maria"},{"name":"Albert"},{"name":"Joseph"}]}'; 
$res=json_decode($arr); 
$data=array(); 
$data=$res->List; 
for($i=0;$i<sizeof($data);$i++) 
{ 
    echo $data[$i]->name; 
} 
相关问题