我需要帮助来访问位于我的 JSON 数据集中的嵌套数组。这是我的顶级 JSON 数组的第一个条目:
{
"pingFeed": [{
"header": "Get Drinks?",
"picture": "images/joe.jpg",
"location": "Tartine's, SF",
"time": "Tomorrow Night",
"name": "Joe Shmoe",
"pid":
"123441121",
"description": "Let's drop some bills, yal!",
"comments": [{
"author": "Joe S.",
"text": "I'm Thirsty"
},
{
"author": "Adder K.",
"text":
"Uber Narfle"
},
{
"author": "Sargon G.",
"text": "taeber"
},
{
"author": "Randy T.",
"text": "Powdered Sugar"
},
{
"author": "Salvatore D.",
"text":
"Chocolate with Sprinkles"
},
{
"author": "Jeff T.",
"type": "Chocolate"
},
{
"author": "Chris M.",
"text": "Maple"
}],
"joined": false,
"participants": [
"Salvatore G.", "Adder K.", "Boutros G."],
"lat": 37.25,
"long": 122,
"private": true
}]
}
我想知道如何使用以下符号访问评论和参与者数据:
for (var k = 0; k < pingFeed.length ; k++) {
console.log(pingFeed[k].comments);
console.log(pingFeed[k].participants);
}
目前这种形式的点表示法适用于 JSON 数组中的其他条目... 我希望将所有这些数据作为字符串返回。
最佳答案
我不太确定你想做什么,但也许这会为你指明正确的方向:
for (var k = 0; k < pingFeed.length; k++) {
for (var i = 0; i < pingFeed[k].comments.length; i++) {
var oComments = pingFeed[k].comments[i];
console.log( oComments.author + ": " + oComments.text );
}
console.log(pingFeed[k].participants.join(", "));
}
关于javascript - 如何在我的 JSON 数据中引用嵌套数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3704999/