rank这个字段有1-5五个数,我现在需要将其取出,然后放入for循环进行if判断,统计其中1出现了几次,2有几次,3总共有多少次,然后将结果放入map集合中,1为一级,2为二级这样。
{
"code": 200,
"data":{
"records":[
{
"id":917834041955188736,
"alertTime":"2021-12-07 17:44:48",
"messageType":3,
"snapId":101124,
"rank":"1",
"alertText":"低电压穿越保护",
},
{
"id":917834020232888320,
"alertTime":"2021-12-07 17:44:42",
"messageType":1,
"snapId":101124,
"rank":"3",
"alertText":"低电压穿越保护",
},
{
"id":917833742737735680,
"alertTime":"2021-12-07 17:43:36",
"messageType":3,
"snapId":101124,
"rank":"2",
"alertText":"低电压穿越保护",
},
{
"id":917833717546745856,
"alertTime":"2021-12-07 17:43:30",
"messageType":1,
"snapId":101124,
"rank":"3",
"alertText":"低电压穿越保护",
}
]
}
}
JSONArray recordList = data.getJSONArray("records");
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
for (int i = 0; i < recordList.size(); i++) {
JSONObject record = recordList.getJSONObject(i);
String ranks = record.getString("rank");
if (StringUtils.isEmpty(ranks))
continue;
if (ranks.equals("1")){
a += record.getInteger("a");
}else if (ranks.equals("2")){
b += record.getInteger("b");
}else if (ranks.equals("3"));{
c += record.getInteger("c");
}
}
JSONObject maps = new JSONObject();
maps.put("一级",a);
maps.put("二级",b);
maps.put("三级",c);
maps.put("四级",d);
maps.put("五级",e);