Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  197] [ 3]  / answers: 1 / hits: 117819  / 14 Years ago, sat, june 12, 2010, 12:00:00

I'm trying to use jQuery's each loop to go through this JSON and add it to a div named #contentHere. The JSON is as follows:



{ justIn: [
{ textId: 123, text: Hello, textType: Greeting },
{ textId: 514, text:What's up?, textType: Question },
{ textId: 122, text:Come over here, textType: Order }
],
recent: [
{ textId: 1255, text: Hello, textType: Greeting },
{ textId: 6564, text:What's up?, textType: Question },
{ textId: 0192, text:Come over here, textType: Order }
],
old: [
{ textId: 5213, text: Hello, textType: Greeting },
{ textId: 9758, text:What's up?, textType: Question },
{ textId: 7655, text:Come over here, textType: Order }
]
}


I'm getting this JSON through use of this code:



$.get(data.php, function(data){

})


Any solutions?


More From » jquery

 Answers
191

Try (untested):



$.getJSON(data.php, function(data){
$.each(data.justIn, function() {
$.each(this, function(k, v) {
alert(k + ' ' + v);
});
});
$.each(data.recent, function() {
$.each(this, function(k, v) {
alert(k + ' ' + v);
});
});
$.each(data.old, function() {
$.each(this, function(k, v) {
alert(k + ' ' + v);
});
});
});


I figured, three separate loops since you'll probably want to treat each dataset differently (justIn, recent, old). If not, you can do:



$.getJSON(data.php, function(data){
$.each(data, function(k, v) {
alert(k + ' ' + v);
$.each(v, function(k1, v1) {
alert(k1 + ' ' + v1);
});
});
});

[#96520] Wednesday, June 9, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rianna

Total Points: 67
Total Questions: 113
Total Answers: 113

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;