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

转载于:https://www.cnblogs.com/onthewaytogrowth/p/11212530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值