如何从嵌套在 JSON 中的数组中检索值
How to retrieve value from array nested in JSON
我想知道如何使用 jQuery 或纯 JavaScript.
从我的 JSON 对象访问 "result 2" 的值
var jsonarray = [{
title: "Kategori1",
items: [{
result: "Item1",
//Nested array
items2: [{
//How to get the value of result2
result2: "item1 item1"
}],
}, {
result: "Item2"
}, {
result: "Item3"
}]
}];
如果我运行console.log(this.data.items[i].items2);
我得到:
[Object { result2="item1 item1"}]
[Object { result2="item4 item4"}]
...在控制台中,但之后我卡住了。
我试过:
this.data.items[i].items2.result2; // Not working
还有另一个循环:
for (var i = 0; i < this.data.items.length; ++i) {
this.data.items[i].result, // Gives me the result from the items array, working
for (var items2 in this.data.items[i]) {
var result = this.data.items[i]['items2'];
console.log(result.result2); //Not working
};
}
试试这个:
this.data.items[i].items2[0].result2;
items2
是一个数组,必须通过索引
访问
this.data.items[0].items2[0].result2
此外,您的 JSON 格式不正确。你有尾随逗号。
var data = JSON.stringify({
title: "Kategori1",
items:
[
{
result: "Item1",
items2: [
{
result2: "item1 item1"
}]
},
{
result: "Item2"
},
{
result: "Item3"
}
]
});
var json = JSON.parse(data);
alert(json.items[0].items2[0].result2);
正如我已经评论过的,您在方括号结尾之前有尾随逗号。请删除以使您的 JSON
有效。
像下面这样尝试。
解决方案 1:无需迭代直接访问。
document.write(jsonarray[0].items[0].items2[0].result2);
方案二:迭代方案
var jsonarray = [{
title: "Kategori1",
items: [{
result: "Item1",
//Nested array
items2: [{
//How to get the value of result2
result2: "item1 item1"
}],
}, {
result: "Item2"
}, {
result: "Item3"
}]
}];
// Iterate jsonarray Array.
for(var i = 0; i < jsonarray.length; i++) {
// Iterating Array Element: Items.
var items = jsonarray[i].items;
for(var j = 0; j < items.length; j++) {
var itemsItem2 = items[j].items2;
// Checking whether Item2 is exist and variable type is Array.
if(itemsItem2 && itemsItem2.constructor == Array) {
// Iterating Items items2.
for(var k = 0; k < itemsItem2.length; k++) {
document.write(itemsItem2[k].result2);
}
}
}
}
我之前的回答是在您实际 JSON 时写的,但我正在编辑它以满足您的标准。
您有一个 JavaScript 数组,而不是 JSON。要访问项目 result2
,您可以执行这行代码:
console.log(jsonarray[0].items[0].items2[0].result2);
因为这不是 JSON,您不需要引用 data
对象。
我想知道如何使用 jQuery 或纯 JavaScript.
从我的 JSON 对象访问 "result 2" 的值var jsonarray = [{
title: "Kategori1",
items: [{
result: "Item1",
//Nested array
items2: [{
//How to get the value of result2
result2: "item1 item1"
}],
}, {
result: "Item2"
}, {
result: "Item3"
}]
}];
如果我运行console.log(this.data.items[i].items2);
我得到:
[Object { result2="item1 item1"}]
[Object { result2="item4 item4"}]
...在控制台中,但之后我卡住了。
我试过:
this.data.items[i].items2.result2; // Not working
还有另一个循环:
for (var i = 0; i < this.data.items.length; ++i) {
this.data.items[i].result, // Gives me the result from the items array, working
for (var items2 in this.data.items[i]) {
var result = this.data.items[i]['items2'];
console.log(result.result2); //Not working
};
}
试试这个:
this.data.items[i].items2[0].result2;
items2
是一个数组,必须通过索引
this.data.items[0].items2[0].result2
此外,您的 JSON 格式不正确。你有尾随逗号。
var data = JSON.stringify({
title: "Kategori1",
items:
[
{
result: "Item1",
items2: [
{
result2: "item1 item1"
}]
},
{
result: "Item2"
},
{
result: "Item3"
}
]
});
var json = JSON.parse(data);
alert(json.items[0].items2[0].result2);
正如我已经评论过的,您在方括号结尾之前有尾随逗号。请删除以使您的 JSON
有效。
像下面这样尝试。
解决方案 1:无需迭代直接访问。
document.write(jsonarray[0].items[0].items2[0].result2);
方案二:迭代方案
var jsonarray = [{
title: "Kategori1",
items: [{
result: "Item1",
//Nested array
items2: [{
//How to get the value of result2
result2: "item1 item1"
}],
}, {
result: "Item2"
}, {
result: "Item3"
}]
}];
// Iterate jsonarray Array.
for(var i = 0; i < jsonarray.length; i++) {
// Iterating Array Element: Items.
var items = jsonarray[i].items;
for(var j = 0; j < items.length; j++) {
var itemsItem2 = items[j].items2;
// Checking whether Item2 is exist and variable type is Array.
if(itemsItem2 && itemsItem2.constructor == Array) {
// Iterating Items items2.
for(var k = 0; k < itemsItem2.length; k++) {
document.write(itemsItem2[k].result2);
}
}
}
}
我之前的回答是在您实际 JSON 时写的,但我正在编辑它以满足您的标准。
您有一个 JavaScript 数组,而不是 JSON。要访问项目 result2
,您可以执行这行代码:
console.log(jsonarray[0].items[0].items2[0].result2);
因为这不是 JSON,您不需要引用 data
对象。