Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to get json data using jquery

    How can I get the comments from below json data?

    Code:
    {
    "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

    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?
    Last edited by doforumda; 09-24-2011 at 04:22 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •