How do i iterate through the json output in JS?

Discussion in 'Web Design, HTML And CSS' started by pank001, Oct 25, 2011.

  1. pank001

    pank001 New Member

    Joined:
    Jul 3, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Dewas, India
    Home Page:
    http://innovate.tk
    I have problems in iterating through the JSON output as the output is


    { "posts" : { "id":"152", "user":"Pankaj Patel", "to":"Pankaj Patel", "msg":"hell!" }
    "posts" : { "id":"151", "user":"Pankaj Patel", "to":"Pankaj Patel", "msg":"hello!" }
    "posts" : { "id":"147", "user":"Pankaj Patel", "to":"Pankaj Patel", "msg":"hey!" }
    }

    and i am not able to iterate in this JSON output!
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    var json = { "posts" : { "id":"152", "user":"Pankaj Patel", "to":"Pankaj Patel", "msg":"hell!" }
    "posts" : { "id":"151", "user":"Pankaj Patel", "to":"Pankaj Patel", "msg":"hello!" }
    "posts" : { "id":"147", "user":"Pankaj Patel", "to":"Pankaj Patel", "msg":"hey!" }
    };
    
    for(var i in json) {
    	for(var j in json[i]) {
    		// print
    	}
    }
    
    
     
  3. pank001

    pank001 New Member

    Joined:
    Jul 3, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Dewas, India
    Home Page:
    http://innovate.tk

    please elaborate above method as the suggested way didn't work!!??
    suppose I want to show output of
    Code:
    <li id="151">"Alice" to "Bob" : "hello!"</li>
    then what would be the method!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice