如何在JSON数据中引用嵌套数组?

时间:2021-10-25 06:43:54

I need assistance in accessing a nested array located my JSON Data Set. Here is the first entry of my top-level JSON array:

我需要帮助访问位于我的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
    }]
}

I would like to know how I can access the comments and participants data using the following notation:

我想知道如何使用以下表示法访问评论和参与者数据:

for (var k = 0; k < pingFeed.length ; k++) {
    console.log(pingFeed[k].comments);
    console.log(pingFeed[k].participants);
 }

Currently this form of dot notation is working for the other entries in the JSON array... I am looking to return all of these data as Strings.

目前这种形式的点符号适用于JSON数组中的其他条目...我希望将所有这些数据作为字符串返回。

3 个解决方案

#1


1  

I'm not sure quite what you're looking to do, but perhaps this will point you in the right direction:

我不太确定你要做什么,但也许这会指出你正确的方向:

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(", "));
}

#2


1  

Well, comments and participants are arrays, so you can access them like normal arrays, e.g.:

好吧,注释和参与者是数组,所以你可以像普通数组一样访问它们,例如:

for (var k = 0; k < pingFeed.length ; k++) {
    var comments = pingFeed[k].comments;
    for(var i = 0, length = comments.length; i < length; ++i) {
        console.log(comments[i]);
    }
}

#3


0  

There's nothing wrong with your code: pingFeed[k].comments will return array and pingFeed[k].comments[0] will return first comment from that array.

您的代码没有任何问题:pingFeed [k] .comments将返回数组,而pingFeed [k] .comments [0]将返回该数组的第一条注释。

Try here
http://jsfiddle.net/U8udd/

试试这里http://jsfiddle.net/U8udd/

#1


1  

I'm not sure quite what you're looking to do, but perhaps this will point you in the right direction:

我不太确定你要做什么,但也许这会指出你正确的方向:

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(", "));
}

#2


1  

Well, comments and participants are arrays, so you can access them like normal arrays, e.g.:

好吧,注释和参与者是数组,所以你可以像普通数组一样访问它们,例如:

for (var k = 0; k < pingFeed.length ; k++) {
    var comments = pingFeed[k].comments;
    for(var i = 0, length = comments.length; i < length; ++i) {
        console.log(comments[i]);
    }
}

#3


0  

There's nothing wrong with your code: pingFeed[k].comments will return array and pingFeed[k].comments[0] will return first comment from that array.

您的代码没有任何问题:pingFeed [k] .comments将返回数组,而pingFeed [k] .comments [0]将返回该数组的第一条注释。

Try here
http://jsfiddle.net/U8udd/

试试这里http://jsfiddle.net/U8udd/