gpt4 book ai didi

java - 从 AJAX 成功函数中检索 ArrayList 元素

转载 作者:行者123 更新时间:2023-12-01 13:50:18 25 4
gpt4 key购买 nike

我有一个servlet,它将以下内容添加到JSONObject中,rscurbatsmen是结果集,clmmodel_batsman是一个具有playerid、playername、playerscore属性的类。我将如何使用 AJAX 函数从 JSP 中检索元素,就像我对其他变量所做的那样。

 ArrayList curbatsmen = new ArrayList(); 
while(rscurbatsmen.next()){

clmmodel_batsman cb = new clmmodel_batsman();
cb.playerid = new String();
cb.playerid = rscurbatsmen.getString("playerid");
cb.playername= new String();
cb.playername = rscurbatsmen.getString("playerfname");
cb.playerscore = new String();
cb.playerscore = rscurbatsmen.getString("runsscored");
curbatsmen.add(cb);

}

json.put("curbatsmen", curbatsmen);

以下是我的 AJAX 代码,我检索了我需要的其他变量

<script type="text/javascript">  
$(document).ready(function(){
(function ajaxInterval() {
$.ajax({
url:'clmcontrol_livematchupdate',
type:'post',
dataType: 'json',

success: function(data) {

$('#lblscore').html(data.score);//lblscore is a label
//Here i need to retrieve elements one by one in order to set the values in particular labels




},
complete: function() {
// Schedule the next request when the current one has been completed
setTimeout(ajaxInterval, 4000);
}
});
})();


});
</script>

最佳答案

在您的 JSON 中应该有一个节点 curbatsmen ,它将计算为一个对象数组,如下所示:

[
{"playerId":1,"playerName":"test","playerScore":"score"},
{"playerId":1,"playerName":"test","playerScore":"score"},
{"playerId":1,"playerName":"test","playerScore":"score"}
]

因此,您可以使用以下代码:

for(i = 0; i < data.curbatsmen.length; i++){
batsman = data.curbatsmen[i];
$("#lblscore").html(batsman.playerScore);

}

或者,无论您实际上想对玩家姓名/分数等做什么。

关于java - 从 AJAX 成功函数中检索 ArrayList 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com