更新時間:2020-07-29 15:54:57 來源:動力節(jié)點 瀏覽8264次
try?{//從數(shù)據(jù)庫中取出List數(shù)據(jù)?
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())?{?
//依次循環(huán)放入對應(yīng)已寫好的類中?
CarouselImageInfo?b?=?new?CarouselImageInfo();?
b.setId(a.getId());?
b.setLink(a.getLink());?
b.setOrderBy(a.getOrderBy());?
b.setAppImageUrl(a.getAppImageUrl());?
list.add(b);?
}?
}?
//轉(zhuǎn)為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;?
}
以上就是動力節(jié)點java培訓(xùn)機構(gòu)的小編針對“JavaList數(shù)據(jù)轉(zhuǎn)換為json類型數(shù)據(jù)”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。
相關(guān)閱讀