前后台传JSON实用方法

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);
}
备注:以上仅供参考,个人开发总结。希望对你有帮助,让我们共同进步。

posted @ 2019-07-19 12:49  不靠黑就看着很壮的人  阅读(811)  评论(0编辑  收藏  举报