admin管理员组

文章数量:1334342

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

{
    "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!",
        "ments": [{
            "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 ments and participants data using the following notation:

for (var k = 0; k < pingFeed.length ; k++) {
    console.log(pingFeed[k]ments);
    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.

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

{
    "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!",
        "ments": [{
            "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 ments and participants data using the following notation:

for (var k = 0; k < pingFeed.length ; k++) {
    console.log(pingFeed[k].ments);
    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.

Share Improve this question edited Sep 14, 2010 at 0:32 Sachin asked Sep 13, 2010 at 22:51 SachinSachin 2,69710 gold badges35 silver badges39 bronze badges 1
  • 2 Tartine is a great restaurant, sorry I couldn't resist. – JP Silvashy Commented Sep 13, 2010 at 23:05
Add a ment  | 

3 Answers 3

Reset to default 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].ments.length; i++) {
        var oComments = pingFeed[k].ments[i];
        console.log( oComments.author + ": " + oComments.text );
    }
    console.log(pingFeed[k].participants.join(", "));
}

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

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

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

Try here
http://jsfiddle/U8udd/

本文标签: javascriptHow do I reference a nested array within my JSON dataStack Overflow