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

success功能如下:通過循環使用jQuery JSON響應

success: function(jsonResponse) { 
} 

請幫我用jQuery循環這一點。謝謝。

+2

歡迎堆棧溢出!爲了獲得最佳效果,請告訴我們您期望發生的事情,展示您嘗試的內容,並解釋該嘗試未能實現您的目標。 –

+0

我可以給你很多鏈接[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) –

+0

alert(JSON.stringify(jsonResponse));我提醒了所有的數組值,但我需要檢索唯一的ID值 – user2615566

回答

2

你將不得不使用$.each或這樣的,假設Items[]有多個對象。 jQuery中有許多迭代函數,如mapeach。選擇一個最適合您的需求。要選擇Items[]jsonResponse,請使用

var items = jsonResponse.Items;

這是一個each功能將如何看起來像:

//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)); 

或者你也可以使用for循環:

//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)); 

或者直接使用map

var ids = $.map(items, function(item, i) { 
    //return id from item object 
    return item.id; 
}); 
+0

感謝您的所有幫助。 – user2615566

+0

歡迎:) @ user2615566 – krishgopinath

1

你可以使用$。每()函數,以遍歷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); 
}); 

這將提醒在項目列表中的每個ID。在這個用例中只有一個對象。

這裏試試:http://jsfiddle.net/mN449/