如何声明JSON并对其进行迭代
How to declare a JSON and iterate through it?
我的JSON有三个数组对象的集合。其中每个数组本身是其他三个对象的集合,总共有九个对象。这是我的JSON:
{
"image1" : [{
"id": "table",
"source": "http://test.reinventio.tk/office-space-game-3/images/table.jpg",
"alert": [],
"explanation": "Tables are needed for maintaining a good posture and consequently, 'n good health - an entrepreneur cant function with a perpetually aching 'n back now, can he?"
}, {
"id": "computer",
"source": "http://test.reinventio.tk/office-space-game-3/images/computer.jpg",
"alert": [],
"explanation": "Whats work without a computer?"
}, {
"id": "ac",
"source": "http://test.reinventio.tk/office-space-game-3/images/ac.jpg",
"alert": [],
"explanation": "Air cons are luxuries. Besides, 'nas an entrepreneur, 'nyoure supposed to be sweating it out!"
}],
"image2" : [{
"id": "sofa",
"source": "http://test.reinventio.tk/office-space-game-3/images/sofa.jpg",
"alert": [],
"explanation": "Sofas are a luxury. Besides, 'nstartups are all but cozy and cusiony, like a sofa!"
}, {
"id": "xbox",
"source": "http://test.reinventio.tk/office-space-game-3/images/xbox.jpg",
"alert": [],
"explanation": "Gaming in the office space is a strict no-no.'n For an entrepreneur, work itself is play!"
}, {
"id": "whiteboard",
"source": "http://test.reinventio.tk/office-space-game-3/images/whiteboard.jpg",
"alert": [],
"explanation": "Wall emulsion paint turns all your walls'n into gigantic whiteboards. So why waste money 'non a small one?"
}],
"image3" : [{
"id": "pool",
"source": "http://test.reinventio.tk/office-space-game-3/images/pooltable.jpg",
"alert": [],
"explanation": "Pool-dreams are to be dreamt after your startup grows."
}, {
"id": "green",
"source": "http://test.reinventio.tk/office-space-game-3/images/inofficeplant.jpg",
"alert": [],
"explanation": "Landscaping in the office?'n Lifes not a golf course - especially 'nfor a startup."
}, {
"id": "paper",
"source": "http://test.reinventio.tk/office-space-game-3/images/paper.jpg",
"alert": [],
"explanation": "Be a jugaadu and improvise to save costs 'n- why would you want to buy paper when you can use the empty side'n of election pamphlets?"
}]
}
现在我想访问每个图像对象的源和id,比如"table"或"ac"。问题是它将图像对象指定为CCD_ 1。
这是我的JavaScript代码:
for(var rowCtr in a.imageTable){
var obj=rowCtr;
for(var colCtr =0;colCtr<obj.length;colCtr++){
var imageObject = obj[colCtr];
imageTable是JSON。
使用for (variable in object)
时,它将variable
设置为对象的键,而不是值。要访问这些值,必须使用object[variable]
。所以在你的情况下,它应该是:
for (var rowCtr in a.imageTable) {
var obj = a.imageTable[rowCtr];
如果变量imageTable
引用了发布的json,那么您将使用id
迭代包含对象的数组,如下所示:
var array = imageTable.image1; // or image2, image3
for(var i=0;i<array.length;i++){
console.log(array[i].id); // wil output table, computer, ac
}
var imageTable = {
"image1" : [{
"id": "table",
"source": "http://test.reinventio.tk/office-space-game-3/images/table.jpg",
"alert": [],
"explanation": "Tables are needed for maintaining a good posture and consequently, 'n good health - an entrepreneur cant function with a perpetually aching 'n back now, can he?"
}, {
"id": "computer",
"source": "http://test.reinventio.tk/office-space-game-3/images/computer.jpg",
"alert": [],
"explanation": "Whats work without a computer?"
}, {
"id": "ac",
"source": "http://test.reinventio.tk/office-space-game-3/images/ac.jpg",
"alert": [],
"explanation": "Air cons are luxuries. Besides, 'nas an entrepreneur, 'nyoure supposed to be sweating it out!"
}],
"image2" : [{
"id": "sofa",
"source": "http://test.reinventio.tk/office-space-game-3/images/sofa.jpg",
"alert": [],
"explanation": "Sofas are a luxury. Besides, 'nstartups are all but cozy and cusiony, like a sofa!"
}, {
"id": "xbox",
"source": "http://test.reinventio.tk/office-space-game-3/images/xbox.jpg",
"alert": [],
"explanation": "Gaming in the office space is a strict no-no.'n For an entrepreneur, work itself is play!"
}, {
"id": "whiteboard",
"source": "http://test.reinventio.tk/office-space-game-3/images/whiteboard.jpg",
"alert": [],
"explanation": "Wall emulsion paint turns all your walls'n into gigantic whiteboards. So why waste money 'non a small one?"
}],
"image3" : [{
"id": "pool",
"source": "http://test.reinventio.tk/office-space-game-3/images/pooltable.jpg",
"alert": [],
"explanation": "Pool-dreams are to be dreamt after your startup grows."
}, {
"id": "green",
"source": "http://test.reinventio.tk/office-space-game-3/images/inofficeplant.jpg",
"alert": [],
"explanation": "Landscaping in the office?'n Lifes not a golf course - especially 'nfor a startup."
}, {
"id": "paper",
"source": "http://test.reinventio.tk/office-space-game-3/images/paper.jpg",
"alert": [],
"explanation": "Be a jugaadu and improvise to save costs 'n- why would you want to buy paper when you can use the empty side'n of election pamphlets?"
}]
};
var array = imageTable.image1; // or image2, image3
for(var i=0;i<array.length;i++){
console.log(array[i].id); // wil output table, computer, ac
}
您可以将其扩展为使用嵌套循环迭代所有imageX
对象
var keys = Object.keys(imageTable);
for(var i=0;i<keys.length;i++){
var array = imageTable[keys[i]];
for(var j=0;j<array.length;j++){
console.log(array[j].id); // wil output table, computer, ac, sofa, xbox etc
}
}
var imageTable = {
"image1" : [{
"id": "table",
"source": "http://test.reinventio.tk/office-space-game-3/images/table.jpg",
"alert": [],
"explanation": "Tables are needed for maintaining a good posture and consequently, 'n good health - an entrepreneur cant function with a perpetually aching 'n back now, can he?"
}, {
"id": "computer",
"source": "http://test.reinventio.tk/office-space-game-3/images/computer.jpg",
"alert": [],
"explanation": "Whats work without a computer?"
}, {
"id": "ac",
"source": "http://test.reinventio.tk/office-space-game-3/images/ac.jpg",
"alert": [],
"explanation": "Air cons are luxuries. Besides, 'nas an entrepreneur, 'nyoure supposed to be sweating it out!"
}],
"image2" : [{
"id": "sofa",
"source": "http://test.reinventio.tk/office-space-game-3/images/sofa.jpg",
"alert": [],
"explanation": "Sofas are a luxury. Besides, 'nstartups are all but cozy and cusiony, like a sofa!"
}, {
"id": "xbox",
"source": "http://test.reinventio.tk/office-space-game-3/images/xbox.jpg",
"alert": [],
"explanation": "Gaming in the office space is a strict no-no.'n For an entrepreneur, work itself is play!"
}, {
"id": "whiteboard",
"source": "http://test.reinventio.tk/office-space-game-3/images/whiteboard.jpg",
"alert": [],
"explanation": "Wall emulsion paint turns all your walls'n into gigantic whiteboards. So why waste money 'non a small one?"
}],
"image3" : [{
"id": "pool",
"source": "http://test.reinventio.tk/office-space-game-3/images/pooltable.jpg",
"alert": [],
"explanation": "Pool-dreams are to be dreamt after your startup grows."
}, {
"id": "green",
"source": "http://test.reinventio.tk/office-space-game-3/images/inofficeplant.jpg",
"alert": [],
"explanation": "Landscaping in the office?'n Lifes not a golf course - especially 'nfor a startup."
}, {
"id": "paper",
"source": "http://test.reinventio.tk/office-space-game-3/images/paper.jpg",
"alert": [],
"explanation": "Be a jugaadu and improvise to save costs 'n- why would you want to buy paper when you can use the empty side'n of election pamphlets?"
}]
};
var keys = Object.keys(imageTable);
for(var i=0;i<keys.length;i++){
var array = imageTable[keys[i]];
for(var j=0;j<array.length;j++){
console.log(array[j].id); // wil output table, computer, ac, sofa, xbox etc
}
}
像这样,您可以访问每个主题的id和源
var id = [];
var src= [];
for (var rowCtr in a.imageTable) {
id= a.imageTable[rowCtr].id;
src= a.imageTable[rowCtr].source;
}
相关文章:
- 如何在Jquery中迭代JSON数组
- 如何迭代json对象结构以只获取名称
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- 如何迭代json对象的嵌套属性并创建新的数组列表
- 迭代JSON对象并将元素打印到屏幕上的最佳方式是什么
- 无法使用jquery迭代json对象
- 使用嵌入式数组迭代JSON
- 如何使用 D3 javascript 迭代 JSON 文件中的数据
- 如何解析 JSON 字符串和迭代 JSON 对象
- 如何使用Jade和Node.js迭代JSON数组
- 访问特定属性并在 AngularJS 中迭代 JSON 数组
- 如何在 jsRender 模板中迭代 JSON 数组
- 如何使用jQuery迭代json数组和拉取值
- Javascript - 递归 - 迭代 json,用户能够跳过
- 翡翠混合迭代 json
- 从 stomp 消息响应 (JavaScript) 迭代 json 数组
- 在 coffeescript 中迭代 json 响应文本
- 迭代 json 响应
- Javascript 按索引迭代 json
- 在 Angular 项目中迭代 JSON 响应以创建新的 $scope obj 我可以在应用程序中使用的