-1
 {
    "Items": [{
        "id": "96",
        "user_id": "1090",
        "points": "120",
        "payment_id": null,
        "reason_id": "1",
        "reference": "",
        "point_date": ""
    }]
}

My success function looks like this :

success: function(jsonResponse) {
}

Please help me on looping this with jQuery. Thanks.

4
  • 2
    Welcome to Stack Overflow! For best results, please tell us what you expect to happen, show what you have tried, and explain how that attempt failed to meet your goal. Commented Jul 24, 2013 at 16:57
  • i can give you many links this, this and this Commented Jul 24, 2013 at 16:59
  • alert(JSON.stringify(jsonResponse)); i have alerted all the array values but i need to retrieve only id values Commented Jul 24, 2013 at 16:59
  • jsonResponse[0].id will give you the first id value Commented Jul 24, 2013 at 17:01

2 Answers 2

2

You'll have to use $.each or such, assuming Items [] has multiple objects. There are many iterative functions in jQuery like map and each. Choose one which best fits your needs. To select Items [] from jsonResponse, use

var items = jsonResponse.Items;

This is how an each function will look like :

//empty array for your ids
var ids = [];
//each function for iteration
$.each(items, function(i, item) {
  ids.push(item.id);
});
//done. now check if its all there 
alert(JSON.stringify(ids));

Or you could also use for loop :

//empty array for your ids
var ids = [], i=0;
//for loop for iteration
for( ; i < items.length, i++)
{
  ids.push(items[i].id);
};
//done. now check if its all there 
alert(JSON.stringify(ids));

Or, simply use map :

var ids = $.map(items, function(item, i) {
     //return id from item object
     return item.id;
});
Sign up to request clarification or add additional context in comments.

Comments

1

You could use the $.each() function in order to traverse the JSON:

var jsonData = {"Items": [{"id":"96","user_id":"1090","points":"120","payment_id":null,"reason_id":"1","reference":"","point_date":""}]}

$(jsonData.Items).each(function(){
    alert(this.id);
});

This would alert each id in the Items list. In this use case there is only one object.

Try it here: http://jsfiddle.net/mN449/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.