1.后台給前台傳list
前台用JSTL,EL獲取的話直接可傳List
前台用JQuary,需要轉成JSON再傳
JsonArray.fromObject(Object object).toString();
2.前台給后台傳Json
需要解析取值 以下是部分代碼:
if (StringUtils.isNotBlank(serviceJsonList)) {
//先轉為JSONArray
JSONArray jsonArray = JSONArray.fromObject(serviceJsonList);
int sumPrice = 0;
int sumCount = 0;
//再遍歷JSONArray
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//判斷JSONObject中是否key都存在
if (jsonObject.containsKey("categoryId") && jsonObject.containsKey("count") && jsonObject.containsKey("groupName")) {
//從JSONObject中取值
int categoryPrice = repairCategoryService.getrepairPriceByid(jsonObject.getInt("categoryId"));
int count = jsonObject.getInt("count");
int price = categoryPrice * count;
sumPrice += price;
sumCount += count;
//JSONObject中值設置到實體類中
ReferredPrimary referredPrimary = (ReferredPrimary) JSONObject.toBean(jsonObject, ReferredPrimary.class);
referredPrimary.setReferredId(AutoincrementreferredId);
//添加主服務項 套餐:主服務項 1:n
referredPrimaryService.insertPrimary(referredPrimary);
}
}
//把配的套餐原價插入
referredSetmealService.insertPrice(AutoincrementreferredId, sumPrice);
//把配的套餐總次數插入
referredSetmealService.insertCategoryCount(AutoincrementreferredId, sumCount);
}
備注:以上僅供參考,個人開發總結。希望對你有幫助,讓我們共同進步。