更新時間:2020-07-29 15:54:57 來源:動力節點 瀏覽7546次
try?{//從數據庫中取出List數據?
List?carouselImageList?=?(List)?this.carouselImageService.carouselImageList();?
List?list?=?new?ArrayList();?//?
for?(int?i?=?0;?i?<?carouselImageList.size();?i++)?{?
CarouselImage?a?=?carouselImageList.get(i);?
if?(a?!=?null?&&?a?!=?new?CarouselImage())?{?
//依次循環放入對應已寫好的類中?
CarouselImageInfo?b?=?new?CarouselImageInfo();?
b.setId(a.getId());?
b.setLink(a.getLink());?
b.setOrderBy(a.getOrderBy());?
b.setAppImageUrl(a.getAppImageUrl());?
list.add(b);?
}?
}?
//轉為Json?
JSONArray?jsonList?=?JSONArray.fromObject(list);?
JSONObject?obj?=?new?JSONObject();?
JSONObject?j?=?new?JSONObject();?
obj.accumulate("carouselImageList",?jsonList);?
j.accumulate("resultCode",?"1");?
j.accumulate("responseObject",?obj);?
PrintWriter?pw?=?response.getWriter();?
pw.print(j);?
}?catch?(Exception?e)?{?
JSONObject?j?=?new?JSONObject();?
try?{?
PrintWriter?pw?=?response.getWriter();?
JSONObject?errObj?=?new?JSONObject();?
j.accumulate("resultCode",?"0");?
j.accumulate("errorMessage",?e.getMessage());?
pw.print(j);?
}?catch?(IOException?e1)?{?
//?TODO?Auto-generated?catch?block?
e1.printStackTrace();?
}?
}?
return?null;?
}
以上就是動力節點java培訓機構的小編針對“JavaList數據轉換為json類型數據”的內容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業老師隨時為你服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習