Eman Resu Dilav
Eman Resu Dilav

Reputation: 3

Parsing JSON with jQuery

I've got this JSON object:

({a1:-1,a2:null, messages:[{b1:message1, b2:message2, b3:message3, ... }]}) 

How do I loop through the messages pairs using jQuery's .each (without hardcoding the b1,b2,b3,message1,message2,message3).

Upvotes: 0

Views: 520

Answers (2)

gilly3
gilly3

Reputation: 91467

Start by reading the reference for jQuery.each(). Here's an apt example from the reference:

$.each({ name: "John", lang: "JS" }, function(k, v) {
    alert( "Key: " + k + ", Value: " + v );
});

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227200

Assuming data is your object, you can use $.each for this.

var messages = data.messages;
$.each(messages, function(i, msg){
   $.each(msg, function(key, message){
      console.log(key+': '+message);
   });
});

Upvotes: 1

Related Questions