访问第二个数组使用jQuery一个JSON德code [英] Accessing second array in a JSON decode using Jquery
问题描述
我需要从 JSON德codeD
串访问第二个数组,但我有没有运气。
整个JSON字符串显示在 VAR RAW00
,然后分裂成 VAR RAW01
&安培; VAR RAW02
。
这些全部3是用于测试 - RAW00等同于味精
当他们被分成 - 我可以访问任,视我开始用什么变化,但是当我使用 RAW00
我无法访问导师节
如果需要,我会提供更多的细节,但我的问题是:
如何查看,并在第二个 $。每个
(嵌套)块访问导师阵列?? ]
感谢: - )
成功:函数(MSG)
{
VAR测试=;
VAR raw00 = {
ALLDATA:[
{
类别2:
{
TID:1,
名:星期一2
},
{
TID:1,
名:周一测试
}
]
},
{
家教:
{
其中fname:杰弗里
LNAME:克拉嫩堡
},
{
其中fname:杰弗里
LNAME:克拉嫩堡
}
]
}
]
}; VAR raw01 = {
ALLDATA:[
{
类别2:
{
TID:1,
名:星期一2
},
{
TID:1,
名:周一测试
}
]
}
]
};
VAR raw02 = {
ALLDATA:[
{
家教:
{
其中fname:杰弗里
LNAME:克拉嫩堡
},
{
其中fname:杰弗里
LNAME:克拉嫩堡
}
]
}
]
}; $。每个(raw00.allData,函数(指数,进入)
{
$。每个(entry.class2,功能(索引,数据)
{
的console.log(this.name);
测试+ ='< TR>< TD>'+ this.name +'< / TD>';
}); $。每个(entry.tutor,功能(索引,数据)
{
的console.log(this.fname);
测试+ ='< TD>'+ this.name +'< / TD>< / TR>';
}); $('#全课程表内容)HTML(测试)。
});
您需要检查数组的当前元素是否与类2
属性或对象导师
属性。
$。每个(raw00.allData,函数(指数,进入){
如果(entry.hasOwnProperty('类别2')){
$。每个(entry.class2,功能(索引,数据)
{
的console.log(this.name);
测试+ ='< TR>< TD>'+ this.name +'< / TD>';
});
} 如果(entry.hasOwnProperty('导师')){
$。每个(entry.tutor,功能(索引,数据)
{
的console.log(this.fname);
测试+ ='< TD>'+ this.fname +'< / TD>< / TR>';
});
} $('#全课程表内容)HTML(测试)。
});
观光很可能是更简单的,如果你重新设计的数据结构。它通常没有意义有对象的数组时,每个对象只是有一个键,它是为每个不同的。我建议你用一个对象替换 ALLDATA
阵列,像这样的:
VAR raw00 = {
ALLDATA:{
类别2:
{
TID:1,
名:星期一2
},
{
TID:1,
名:周一测试
}
]
家教:
{
其中fname:杰弗里
LNAME:克拉嫩堡
},
{
其中fname:杰弗里
LNAME:克拉嫩堡
}
]
}
};
I need to access the second array from a JSON decoded
string, but I am having no luck.
The entire JSON string is displayed in var RAW00
, and then split into var RAW01
& var RAW02
.
All 3 of these are for testing - RAW00 is identical to msg
When they are split - I can access either, depending on what variable I start of with, but when I use RAW00
I cannot access the tutor section.
I will provide more detail if required, but my question is:
How do I see and access the tutor array in the second $.each
(nested) block??]
Thanks :-)
success: function(msg)
{
var test = "";
var raw00 = {
"allData": [
{
"class2": [
{
"tid": "1",
"name": "Monday 2"
},
{
"tid": "1",
"name": "Monday Test"
}
]
},
{
"tutor": [
{
"fname": "Jeffrey",
"lname": "Kranenburg"
},
{
"fname": "Jeffrey",
"lname": "Kranenburg"
}
]
}
]
};
var raw01 = {
"allData": [
{
"class2": [
{
"tid": "1",
"name": "Monday 2"
},
{
"tid": "1",
"name": "Monday Test"
}
]
}
]
};
var raw02 = {
"allData": [
{
"tutor": [
{
"fname": "Jeffrey",
"lname": "Kranenburg"
},
{
"fname": "Jeffrey",
"lname": "Kranenburg"
}
]
}
]
};
$.each(raw00.allData, function(index, entry)
{
$.each(entry.class2, function (index, data)
{
console.log(this.name);
test += '<tr><td>'+this.name+'</td>';
});
$.each(entry.tutor, function (index, data)
{
console.log(this.fname);
test += '<td>'+this.name+'</td></tr>';
});
$('#all-courses-table-content').html( test );
});
You need to check whether the current element of the array is an object with class2
property or tutor
property.
$.each(raw00.allData, function(index, entry) {
if (entry.hasOwnProperty('class2')) {
$.each(entry.class2, function (index, data)
{
console.log(this.name);
test += '<tr><td>'+this.name+'</td>';
});
}
if (entry.hasOwnProperty('tutor')) {
$.each(entry.tutor, function (index, data)
{
console.log(this.fname);
test += '<td>'+this.fname+'</td></tr>';
});
}
$('#all-courses-table-content').html( test );
});
Things would probably be simpler if you redesigned the data structure. It generally doesn't make sense to have an array of objects when each object just has a single key and it's different for each. I suggest you replace the allData
array with a single object, like this:
var raw00 = {
"allData": {
"class2": [
{
"tid": "1",
"name": "Monday 2"
},
{
"tid": "1",
"name": "Monday Test"
}
],
"tutor": [
{
"fname": "Jeffrey",
"lname": "Kranenburg"
},
{
"fname": "Jeffrey",
"lname": "Kranenburg"
}
]
}
};
这篇关于访问第二个数组使用jQuery一个JSON德code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!