如何在JSON中返回数组格式的数据?
·
一、发出请求
public String test(){
List<CompanyResPool> list=new ArrayList<CompanyResPool>();
CompanyResPool comResPool = new CompanyResPool();
String companyIdStr=companyId+"";
int vrBandwidth=0;
String ip_num="0";
List<String> ipList=new ArrayList<String>();
String vr_service="暂无";
List<String> vr_eip=new ArrayList<String>();
List<String> vr_snat=new ArrayList<String>();
List<String> vr_dnat=new ArrayList<String>();
List<String> vr_total=new ArrayList<String>();
String vr_userNum="0";
String vr_totalNum="0";
String Url2 = "http://" +nodeIp+ "/BackManagerNode/VrouterApi_getBandwithByCompanyId";
String Str2 = HttpRequest.sendGet(Url2, "companyId="+companyIdStr+"&poolid="+comResPool.getId());
if (StringUtils.isNotBlank(Str2)) {
JSONObject resultObj = JSONObject.fromObject(Str2);
vrBandwidth = resultObj.getInt("VR_BandWidth");
vr_service=resultObj.getString("VR_Service");
vr_userNum=resultObj.getString("IP_usedNum");
vr_totalNum=resultObj.getString("IP_totalNum");
vr_eip=(List<String>)resultObj.get("VR_eip");
vr_snat=(List<String>)resultObj.get("VR_snatip");
vr_dnat=(List<String>)resultObj.get("VR_dnatip");
vr_total=(List<String>)resultObj.get("IP_total");
}
others.put("vrBandwidth", vrBandwidth);
others.put("vr_service", vr_service);
others.put("vr_eip", vr_eip);
others.put("vr_snat", vr_snat);
others.put("vr_dnat", vr_dnat);
others.put("IP_usedNum", vr_userNum);
others.put("IP_total", vr_total);
others.put("IP_totalNum", vr_totalNum);
comResPool.setOthers(others);
list.add(comResPool);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class,
new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
jsonConfig.setExcludes(new String[]{"node_capacity"});
generateSuccessListResponse(JSONArray.fromObject(newComResList,jsonConfig));
return JSON;
}
二、处理请求
// 根据companyId查询Bandwidth
public void getBandwithByCompanyId() {
JSONObject json = new JSONObject();
VRouter vr=vRouterService.getBandwithByCompanyId(companyId+"");
//获取分配给SLB的IP
List<Ipv4Entity> list = ipv4Service.getByPoolId(poolid+"");
List<String> ipList=new ArrayList<String>();
if(list!=null&&list.size()!=0){
for(Ipv4Entity ip:list){
ipList.add(ip.getIpv4_address());
}
}
List<String> dnatIp=new ArrayList<String>();
List<String> eipIp=new ArrayList<String>();
List<String> snatIp=new ArrayList<String>();
if(vr!=null){
VRouterEntity vRouterEntity = vRouterEntityService.getByUUID(vr.getUUID());
boolean flag=false;
String str="";
//公网:
//1.eip(若同时有eip和dnat,显示功能为eip)
List<EIPInfoEntity> eipList = eipInfoService.getByVrUUID(vr.getUUID());
for(int i=0;i<eipList.size();i++){
if (eipList.get(i) != null && "1".equals(eipList.get(i).getStatus())) {
eipIp.add(eipList.get(i).getWanip());
}
}
//2.dnat(有 多个,也可能为同一个)
List<DnatEntity> dnatList=dnatService.getByVruuid(vr.getUUID());
for(int i=0;i<dnatList.size();i++){
if (dnatList.get(i) != null && "1".equals(dnatList.get(i).getStatus())) {
dnatIp.add(dnatList.get(i).getWanip());
}
}
//3.snat(一个;若同时有dnat和snat,显示功能为snat)
List<SnatEntity> snatList=snatService.getByVruuid(vr.getUUID());
for(int i=0;i<snatList.size();i++){
if(snatList.get(i)!=null&&"1".equals(snatList.get(i).getStatus())){
snatIp.add(snatList.get(i).getWan());
}
}
System.out.println("dnatIp="+dnatIp);
System.out.println("eipIp="+eipIp);
System.out.println("snatIp="+snatIp);
//若dnat有多个,只保留一个
dnatIp=removeSelf(dnatIp);
//若同时有eip和dnat,那么删除dnat里面的ip
dnatIp=removeDuplicate(dnatIp, eipIp);
//若同时有dnat和snat,删除dnat里面的ip
dnatIp=removeDuplicate(dnatIp, snatIp);
json.accumulate("VR_BandWidth", vr.getBandWidth());
//vr的管理网ip
if(vRouterEntity!=null&&(!"".equals(vRouterEntity.getControl_ip()))){
json.accumulate("VR_Service", vRouterEntity.getControl_ip());
}else{
json.accumulate("VR_Service", "暂无");
}
json.accumulate("VR_eip", eipIp);
json.accumulate("VR_snatip", snatIp);
json.accumulate("VR_dnatip", dnatIp);
if(eipIp!=null||snatIp!=null||dnatIp!=null){
json.accumulate("IP_usedNum", eipIp.size()+snatIp.size()+dnatIp.size());
}else{
json.accumulate("IP_usedNum", "0");
}
json.accumulate("IP_total", ipList);
if(ipList!=null&&ipList.size()!=0){
json.accumulate("IP_totalNum", ipList.size());
}else{
json.accumulate("IP_totalNum", "0");
}
}else{
json.accumulate("VR_BandWidth", "0");
json.accumulate("VR_Service", "暂无");
json.accumulate("VR_eip", eipIp);
json.accumulate("VR_snatip", snatIp);
json.accumulate("VR_dnatip", dnatIp);
json.accumulate("IP_usedNum", "0");
json.accumulate("IP_total", ipList);
json.accumulate("IP_totalNum","0");
}
responseData(json.toString());
}
感悟:最近的工作比较多的接触json,发现json在数据传输上去,确实要比一般的List方便很多。页面展示也比较轻松。
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)