[英]ajax: extract json object from array of json
JSON (inside array from api): JSON(来自api的内部数组):
[{"id":"001", "name":"john", "age":"40"}, {"id":"002", "name":"jane", "age":"30"}]
Ajax: 阿贾克斯:
$.ajax({
url: 'interface_API.php',
dataType: 'json',
success: function(data)
{
for(var i in data){
var row = data[i];
var id = row[0];
var name = row[1];
var age = row[2];
$('#output').append("<tr width='50%'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td></tr>");
}
}
});
The API uses this to construct: API使用它来构造:
if(mysqli_num_rows($output)){
while($row = mysqli_fetch_assoc($output)){
$json[] = $row;
}
}
However my Output is 'undefined' repeated for each .append
但是我的输出对于每个.append
都是“未定义”的
How do i extract each json object from the array & append to the page? 我如何从数组中提取每个json对象并追加到页面?
change this: 改变这个:
var id = row[0];
var name = row[1];
var age = row[2];
to this: 对此:
var id = row['id'];
var name = row['name'];
var age = row['age'];
or this: 或这个:
var id = row.id;
var name = row.name;
var age = row.age;
Because you already looped in here: 因为您已经在此处循环:
for(var i in data){
var row = data[i]; // <----here
so you just required to reference it with keys in the js object. 因此,您只需要使用js对象中的键来引用它即可。
You can change the for loop like this: 您可以这样更改for循环:
$.ajax({
url: 'interface_API.php',
dataType: 'json',
success: function(data)
{
for(var i in data){
var row = data[i];
var id = row.id;
var name = row.name;
var age = row.age;
$('#output').append("<tr width='50%'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td></tr>");
}
}
});
please use the key to identify the json Object while retrieving 请在检索时使用密钥标识json对象
var list=[
{"id":"1","text":"test1"},
{"id":"2","text":"test2"},
{"id":"3","text":"test3"},
{"id":"4","text":"test4"}
]
alert(list[2].id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.