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

krishwader
  • 11,341
  • 1
  • 34
  • 51
user2615566
  • 21
  • 1
  • 3
  • 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. – George Cummins Jul 24 '13 at 16:57
  • i can give you many links [this](http://stackoverflow.com/questions/733314/jquery-loop-over-json-result-from-ajax-success), [this](http://stackoverflow.com/questions/10251772/loop-json-response-after-ajax-success) and [this](http://stackoverflow.com/questions/1208270/how-to-loop-through-my-json-response-using-jquery) – Khawer Zeshan Jul 24 '13 at 16:59
  • alert(JSON.stringify(jsonResponse)); i have alerted all the array values but i need to retrieve only id values – user2615566 Jul 24 '13 at 16:59
  • `jsonResponse[0].id` will give you the first id value – Lee Meador Jul 24 '13 at 17:01

2 Answers2

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;
});
krishwader
  • 11,341
  • 1
  • 34
  • 51
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/

Duane
  • 4,460
  • 1
  • 25
  • 27