Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   How do i iterate through the json output in JS? (http://www.go4expert.com/forums/i-iterate-json-output-js-t27014/)

pank001 25Oct2011 11:44

How do i iterate through the json output in JS?
 
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!

pradeep 27Oct2011 10:50

Re: How do i iterate through the json output in JS?
 
Code: Javascript

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
    }
}


pank001 28Oct2011 03:19

Re: How do i iterate through the json output in JS?
 
Quote:

Originally Posted by pradeep (Post 88513)
Code: Javascript

var json = { "posts" : { "id":"152", "user":"Bob", "to":"Alice", "msg":"hell!" }
"posts" : { "id":"151", "user":"Alice", "to":"Bob", "msg":"hello!" }
"posts" : { "id":"147", "user":"Bob", "to":"Alice", "msg":"hey!" }
};

for(var i in json) {
    for(var j in json[i]) {
        // print
    }
}



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!


All times are GMT +5.5. The time now is 15:18.