javascript 在 ajax 响应中循环一个 json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25636839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loop through a json object in ajax response
提问by Dev
I am new to json so i am getting a json reponse from my ajax call
我是 json 的新手,所以我从我的 ajax 调用中得到了 json 响应
now i am stuck with looping the json object
现在我坚持循环 json 对象
here is my json
这是我的 json
{
"0": {
"image": "http://test.com/systems.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "Oct-Dec-2013"
},
"1": {
"image": "http://test.com/energy.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "July-Sept-2013"
},
"pages": 2
}
Can anyone help
谁能帮忙
回答by Dave
You can use a for-in loop as follows:
您可以使用 for-in 循环,如下所示:
var obj = {
"0": {
"image": "http://test.com/systems.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "Oct-Dec-2013"
},
"1": {
"image": "http://test.com/energy.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "July-Sept-2013"
},
"pages": 2
}
for(var prop in obj) {
var item = obj[prop];
console.log(item);
}
Be aware that you will get the items in your log because you will get the pages
property in addition to the numeric properties.
请注意,您将获得日志中的项目,因为pages
除了数字属性之外,您还将获得该属性。
回答by Gowtham Selvaraj
Save your JSON response in a variable
将您的 JSON 响应保存在一个变量中
var variable = {
"0" : {
"image" : "http://test.com/systems.jpg",
"anchor_tag_link" : "http://test.com/1",
"title" : "Oct-Dec-2013"
},
"1" : {
"image" : "http://test.com/energy.jpg",
"anchor_tag_link" : "http://test.com/1",
"title" : "July-Sept-2013"
},
"pages" : 2
};
Then loop it using jquery
然后使用jquery循环它
$.each(variable, function(index, value) {
alert(value.image);
alert(value.anchor_tag_link);
});
回答by Suchit kumar
you can do this.
你可以这样做。
var json = JSON.parse(data);// here data is your response
for (var key in json) {
alert(json[key].image);// other also in the same way.
}
回答by Janani M
JQuery:
var JSON = {
"0": {
"image": "http://test.com/systems.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "Oct-Dec-2013"
},
"1": {
"image": "http://test.com/energy.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "July-Sept-2013"
},
"pages": 2
};
if(JSON.pages >0)
{
for(var i=0; i<JSON.pages; i++)
{
$('table').append('<tr><td>'+JSON[i].title+'</td><td>'+JSON[i].image+'</td><td>'+JSON[i].anchor_tag_link+'</td></tr>');
}
}
HTML:
<table border="1"></table>
回答by ashokhein
Example 1 :
示例 1:
success: function(responseData) {
for (var key in responseData) {
alert(responseData[key]);
}
}
Example 2 :
示例 2:
<script>
var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';
var json = JSON.parse(data);
alert(json["name"]); //mkyong
alert(json.name); //mkyong
alert(json.address.streetAddress); //88 8nd Street
alert(json["address"].city); //New York
alert(json.phoneNumber[0].number); //111 111-1111
alert(json.phoneNumber[1].type); //fax
alert(json.phoneNumber.number); //undefined
</script>
回答by maxime_039
Please try the following code. You only have to replace "yourJSONObject" by the JSON array.
请尝试以下代码。您只需将“yourJSONObject”替换为 JSON 数组。
$.each( yourJSONObject, function( keyImg, valImg ) {
image = valImg[0];
anchor_tag_link = valImg[1];
title = valImg[2];
});
Regards,
问候,
回答by Ammaro
you can loop through this like looping through javascript object :
你可以像循环 javascript 对象一样循环:
for(var arg in object) {
object.hasOwnProperty(arg) {
process(object[arg]);
}
}