/** *JSON 格式的解析 */ // json 去掉转义字符 message = message.replaceAll("\\\\", ""); //转成String类型 String jsonStr = message.substring(message.indexOf("[") + 1, message.indexOf("]")); //转成String类型的JSON格式 jsonObject = JSONObject.fromObject(jsonStr); //通过key进行取值 String responseCode = jsonObject.getString("msg");
mapRes.put("empNameAdd", empNameAdd); mapRes.put("deptNameAdd", deptNameAdd); mapRes.put("deptNo", deptNo); mapRes.put("loginName", loginName); mapRes.put("empId", empId); //定义转成json数组,将map集合封装的数据放到json数组里,定义一个jsonObject, //通过put方法,转给前台 JSONArray ar = new JSONArray(); ar.add(mapRes); JSONObject oj = new JSONObject(); oj.put("memberInfo", ar); _result.setData(oj); /** **{ "status": 0, "msg": "ok", "data": [ "memberInfo":{ { "deptNo": "103", "empNameAdd": "李俊", "empId": "50048", "loginName": "lj", "deptNameAdd": "上海健一网大药房连锁经营有限公司小世界药店" } } ] } */
@RequestMapping("/insertFromStore") public void insertFromStore( HttpServletRequest request, HttpServletResponse response){ StringBuffer json = new StringBuffer(); String line = null; String orderMsg = ""; try { BufferedReader reader = request.getReader(); while((line = reader.readLine()) != null) { json.append(line); } orderMsg = json.toString(); } catch(Exception e) { logger.error(e); } if (StringUtil.isEmpty(orderMsg)){ this.setResultInfo("-1", "params error!").write(request, response); return; } com.alibaba.fastjson.JSONObject jsonObject = null; try { //把String类型的json串转成Json Object对象 jsonObject = com.alibaba.fastjson.JSONObject.parseObject(orderMsg); } catch (Exception e) { this.setResultInfo("-1", "params error!").write(request, response); return; } //门店渠道id int storeMutilId = 46; //各种取值 String mTel = jsonObject.getString("mTel"); String tranid = jsonObject.getString("tranid"); //取值,拿到json数组 com.alibaba.fastjson.JSONArray artiitem = jsonObject.getJSONArray("artiitem"); String tranDate = jsonObject.getString("tranDate"); String discount = jsonObject.getString("discount"); String deptName = jsonObject.getString("deptName"); String sumSaleAmt = jsonObject.getString("sumSaleAmt"); if(StringUtil.isEmpty(mTel)||StringUtil.isEmpty(tranid)||StringUtil.isEmpty(tranDate)||StringUtil.isEmpty (discount)||StringUtil.isEmpty(deptName)||StringUtil.isEmpty(sumSaleAmt)){ this.setResultInfo("-1", "params error!").write(request, response); return; } List<OrderItem> itemList = new ArrayList<OrderItem>(); //订单详情信息 //遍历json数组,取值,赋值 for (Object o : artiitem) { OrderItem item = new OrderItem(); //转成String类型Json String jsonStr = com.alibaba.fastjson.JSONObject.toJSONString(o); //转成json格式 com.alibaba.fastjson.JSONObject itemJson = com.alibaba.fastjson.JSONObject.parseObject(jsonStr); String artiCode = itemJson.getString("artiCode"); String artiQty = itemJson.getString("artiQty"); String saleAmt = itemJson.getString("saleAmt"); String salePrice = itemJson.getString("salePrice"); if(StringUtil.isEmpty(artiCode)||StringUtil.isEmpty(artiQty)||StringUtil.isEmpty(saleAmt)||StringUtil .isEmpty(salePrice)){ this.setResultInfo("-1", "params error!").write(request, response); return; } if(new BigDecimal(saleAmt).compareTo(new BigDecimal(salePrice).multiply(new BigDecimal(artiQty))) != 0){ this.setResultInfo("3", "单品金额校验失败").write(request, response); return; } item.setGoodsNo(artiCode); item.setGoodsPrice(new BigDecimal(salePrice)); item.setGoodsAmount(new BigDecimal(artiQty)); item.setGoodsSumFee(new BigDecimal(saleAmt)); itemList.add(item); } //订单信息 OrderInfo orderInfo = new OrderInfo(); orderInfo.setMemberMobile(mTel); orderInfo.setMultiChannelId(storeMutilId); orderInfo.setMultiChannelOrderId(Long.valueOf(tranid)); orderInfo.setOtherDiscounts(new BigDecimal(discount)); orderInfo.setFinishTime(tranDate); orderInfo.setOrderFee(new BigDecimal(sumSaleAmt)); ServiceMessage<String> insertResult= null; String resultMsg = ResultMsg.Common.OK; try { insertResult= orderInfoService.insertOrderInfoFromStore(orderInfo,itemList,deptName); resultMsg = insertResult.getMessage(); } catch (Exception e) { LOG.error(e.getMessage(), e); resultMsg = ResultMsg.OrderActionMsg.INSERT_STORE_FAILURE; } finally { this.setResult(insertResult, resultMsg).write(request, response); } }