使用jQuery循环浏览JSON结果 [英] loop through JSON result with jQuery
本文介绍了使用jQuery循环浏览JSON结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON响应,但我不确定如何正确循环并使用它。
{
ID:1,
Name:dept1,
Categories:[
{
ID:1,
姓名:catg1
},
{
ID:2,
姓名:catg2
}
]
以下代码提醒我departmentID为1,然后命名为'dept1',然后这:'[object Object],[object Object]'
$ .getJSON(mainPage2.aspx,function(result){
$ .each(result,function(i,item){
alert(this);
});
});
我想要做的是使用部门信息创建一个div,并在其中创建另一个div包括属于该部门的类别信息。
解决方案
for(var key in结果){
var value = result [key];
if(typeof value =='object'){
if(value instanceof Array){
//数组。循环遍历孩子
for(var i = 0; i< value.length; i ++){
var item = value [i];
}
} else {
//复杂对象,而不是数组。内循环键?
}
}其他{
//常规字符串/数字等只是打印出值?
}
}
i have the following JSON response, but i am not sure how to properly loop trough it and use.
{
"ID": 1,
"Name": "dept1",
"Categories": [
{
"ID": 1,
"Name": "catg1"
},
{
"ID": 2,
"Name": "catg2"
}
]
}
following code alerts me the departmentID which is 1, then its name 'dept1', then this: '[object Object],[object Object]'
$.getJSON("mainPage2.aspx", function(result) {
$.each(result, function(i, item) {
alert(this);
});
});
all i want to do is to create a div using department info, and create another div inside it which includes the information of categories that belong to that deparment.
解决方案
for(var key in result) {
var value = result[key];
if(typeof value == 'object') {
if(value instanceof Array) {
// an array. loop through children
for(var i = 0; i < value.length; i++) {
var item = value[i];
}
} else {
// complex object, not array. inner for loop on keys?
}
} else {
// regular string/number etc. just print out value?
}
}
这篇关于使用jQuery循环浏览JSON结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文