[英]Can't get JSON data from array inside of array using for loop in javascript/jquery
I have a problem with getting some data from JSON format. 我从JSON格式获取一些数据时遇到问题。 I already got most of the data, but there is data which is an array inside of an array and it's more complicated. 我已经掌握了大部分数据,但其中的数据是数组内部的一个数组,而且更为复杂。 I can't seem to find out what should I do. 我似乎不知道该怎么办。
The JSON file is like that:(that's a part of it - i can't change it) JSON文件就是这样的:(这是它的一部分-我无法更改)
{
"smth": [
{
"part": {
"d": 295.15
},
"rock": [
{
"description": "heavy",
"icon": "22sd"
}
],
"song": 10
}
{
"part": {
"d": 295.15
},
"rock": [
{
"description": "soft",
"icon": "33sd"
}
],
"song": 10
}
]
}
My code, a part which doesn't work as I want: 我的代码,一部分无法按我的意愿工作:
var icon=[];
var desc=[];
$.getJSON(url, function(json) {
$.each(json.smth, function() {
$.each(this.rock, function() {
for (var i in json.smth){
main[i] = this.description;
icon[i] = this.icon;
}
});
});
document.getElementById('icon1').src='..//img/w/'+icon[0]+'.png';
document.getElementById('icon2').src='..//img/w/'+icon[1]+'.png';
document.getElementById('icon3').src='..//img/w/'+icon[2]+'.png';
document.getElementById('icon4').src='..//img/w/'+icon[3]+'.png';
document.getElementById('icon5').src='..//img/w/'+icon[4]+'.png';
document.getElementById('main1').innerHTML=desc[0];
document.getElementById('main2').innerHTML=desc[1];
document.getElementById('main3').innerHTML=desc[2];
document.getElementById('main4').innerHTML=desc[3];
document.getElementById('main5').innerHTML=desc[4];
});
I need to get data as Array. 我需要获取数据作为数组。 But I can't wrote that part like: this[i].description
. 但是我不能这样写: this[i].description
。 The other data worked well like this: 其他数据如下所示运行良好:
rain[i]=json.smth[i].part.d;
There I didn't need to use 在那里我不需要使用
$.each(json.list, function()
Also, I don't know if it's somehow possible for the last 10 rows in my code to use a loop.. 另外,我不知道代码中的最后10行是否可能使用循环。
Does anyone have any suggestions? 有没有人有什么建议? I'd be really grateful.. 我将非常感激。
Try this 尝试这个
$.getJSON(url, function (json) {
$.each(json.smth, function () {
$.each(this.rock, function (idx) {
document.getElementById('icon' + (idx + 1).toString()).src = '..//img/w/' + this.icon + '.png';
document.getElementById('main' + (idx + 1).toString()).innerHTML = this.description;
}
);
});
});
So if I'm understand what you are trying to do correctly, then you should be able to do this: 因此,如果我了解您要正确执行的操作,那么您应该可以执行以下操作:
$.each(json.smth, function() {
$.each(this.rock, function() {
icon.push(this.icon);
desc.push(this.description);
}
}
And you'll end up with your two arrays populated. 最后,您将填充两个数组。
Personally, if those two items, icon and description, are associated together, I'd rather keep them together in a single object. 就个人而言,如果将图标和说明这两个项目关联在一起,我宁愿将它们放在一个对象中。 Something like: 就像是:
var foo = [];
$.each(json.smth, function() {
$.each(this.rock, function() {
foo.push({icon:this.icon,desc:this.description});
}
}
Which you can now access each component: 您现在可以访问其中的每个组件:
foo[0].icon
foo[0].desc
Now to do your DOM manipulation, you can just loop through your new array (or arrays if you keep them separate): 现在要进行DOM操作,您可以循环遍历新数组(或将数组分开的数组):
for (var i=0; i < foo.length; i++) {
$("#icon" + (i+1)).prop("src","..//img/w/"+foo[i].icon+".png");
$("#main" + (i+1)).html(foo[i].desc);
}
Or as @malkam suggested, if you don't actually need the arrays, you can just do the DOM manipulation right inside your JSON processing loop. 或按照@malkam的建议,如果您实际上不需要数组,则可以在JSON处理循环内进行DOM操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.