0

I am getting the following JSON string from a webservice.

[{"TITLE":"asdasdasd","DESCRIPTION":"asdasd","PORTFOLIOID":1},
 {"TITLE":"sss","DESCRIPTION":"sss","PORTFOLIOID":2},
 {"TITLE":"sdfsdf","DESCRIPTION":"sdfsfsdf","PORTFOLIOID":3}]

Can i loop over this array in jquery and output the individual key/value pairs?

4
  • 4
    And the answer is: Yes you can! Commented May 6, 2011 at 13:21
  • Great, Care to let me kow how? Commented May 6, 2011 at 13:21
  • simple question, simple answer :) Commented May 6, 2011 at 13:22
  • Have a look here: developer.mozilla.org/en/JavaScript/Guide/Statements Commented May 6, 2011 at 13:27

2 Answers 2

2
var a = [{"TITLE":"asdasdasd","DESCRIPTION":"asdasd","PORTFOLIOID":1}, ....]

$(a).each(function(index)
{
   //this is the object in the array, index is the index of the object in the array
   alert(this.TITLE + ' ' this.DESCRIPTION)
});

Check out the jQuery docs for more info... http://api.jquery.com/jQuery.each/

Sign up to request clarification or add additional context in comments.

Comments

1

Absolutely. Assuming you're telling jQuery to evaluate this response as JSON with the AJAX methods, you'd simply do this:

<script>
$(data).each(function(idx, obj) //this loops the array
{
    $(obj).each(function(key, value) //this loops the attributes of the object
    {
        console.log(key + ": " + value);
    }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.