Jump to content

Recommended Posts

How can I get the comments from below json data?

 

{
"data":
[
    {
        "id":"123",
        "from":{"name":"name","id":"12"},
        "message":"Message",
        "comments": {
                        "data":
                        [
                            {
                                "id":"342",
                                "from":{"name":"name","id":"32"},
                                "message":"comment message 1"
                            },
                            {
                                "id":"341",
                                "from":{"name":"name","id":"21"},
                                "message":"comment message 2"
                            }
                        ],
                        "count":2
                    }
    },
    {
        "id":"143",
        "from":{"name":"name","id":"52"},
        "message":"Message",
        "comments": {
                        "data":
                        [
                            {
                                "id":"362",
                                "from":{"name":"name","id":"72"},
                                "message":"comment message 1"
                            },
                            {
                                "id":"341",
                                "from":{"name":"name","id":"41"},
                                "message":"comment message 2"
                            }
                        ],
                        "count":2
                    }
    }
]
}

 

I know how to get id, from and message. but I do not know how can I get data inside comments.

 

here is my jquery code

 

$.getJSON(newsfeed_url,function(d) {
    $.each(d.data, function(i,res) {
        html += "<div class='container'>";
        html += "<li>"+ res.from.name +"</li>";
        html += "<li class='message'>" + res.message + "</li>";
        html += "<li>Comments: " + res.comments.count + "</li>";

        $.each(res.comments.data, function(i, comment) {
            html += "<li class=''>" + comment.message + "</li>";
        });
        html += "</div>";            
    });
    html += "</ul>";
    $("#contents").html(html);
});

 

my current code does get res.from.name, res.comments.count but it does not get data inside comments i.e. res.comments.data.

 

how can I achieve it?

Link to comment
https://forums.phpfreaks.com/topic/247787-how-to-get-json-data-using-jquery/
Share on other sites

The data is in an array, so you would need to access them through the index:

 

res.comments.data[0].id

res.comments.data[1].id

 

Or using the count property you have, you could loop through them:

 

for (i = 0; i < res.comments.count; i++) {
    // res.comments.data[i].id
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.