// 到货操作 @RequestMapping(value = "/addArr.json", method = RequestMethod.POST) @ResponseBody private Object addArr(String purchaseArr, String data) { JSONObject purchase = JSONObject.fromObject(purchaseArr); PurchaseArrival arr = (PurchaseArrival) JSONObject.toBean(purchase, PurchaseArrival.class); JSONArray array = JSONArray.fromObject(data); List<PurchaseOrderDetail> list = new ArrayList<PurchaseOrderDetail>(); for (int i = 0; i < array.toArray().length; i++) { // 遍历循环,去除最后一项统计栏的信息 JSONObject json = JSONObject.fromObject(array.toArray()[i]); PurchaseOrderDetail resourceBean = (PurchaseOrderDetail) JSONObject.toBean(json, PurchaseOrderDetail.class); if (resourceBean.getGoodsCode().equals("<b>统计:</b>")) { continue; } if (arr.getNotPayAmo() == null) { arr.setNotPayAmo(new BigDecimal(0)); } if (arr.getAlrInvAmo() == null) { arr.setAlrInvAmo(new BigDecimal(0)); } if (arr.getNotReturnAmo() == null) { arr.setNotReturnAmo(new BigDecimal(0)); } arr.setNotPayAmo(arr.getNotPayAmo().add(resourceBean.getMoney())); // 未付款金额 arr.setAlrInvAmo(arr.getAlrInvAmo().add(resourceBean.getMoney())); // 已开票金额 arr.setNotReturnAmo(arr.getNotReturnAmo().add(resourceBean.getMoney())); // 未退货金额 list.add(resourceBean); } return purchaseOrderService.addPurchaseArr(arr, list); }
/** * 递归地将JSONArray转换为List对象,将JSONObject转换为Map对象 * * @param obj * @return */ private static Object parseJSONObj(Object obj) { Object result = null; if (obj == null) { // error return null; } else { if (obj instanceof JSONArray) { JSONArray arrayObj = (JSONArray) obj; List<Object> list = new ArrayList<Object>(); for (Object element : arrayObj.toArray()) { list.add(JsonUtil.parseJSONObj(element)); } result = list; } else if (obj instanceof JSONObject) { JSONObject jsonObj = (JSONObject) obj; Map<String, Object> map = new HashMap<String, Object>(); for (Object key : jsonObj.keySet()) { map.put(key.toString(), JsonUtil.parseJSONObj(jsonObj.get(key.toString()))); } return map; } else { result = obj; } } return result; }
// 修改订购合同 @RequestMapping(value = "/edit.json", method = RequestMethod.POST) @ResponseBody private Object editPurchaseOrder(String purchaseOrder, String data) { JSONObject purchase = JSONObject.fromObject(purchaseOrder); PurchaseOrder order = (PurchaseOrder) JSONObject.toBean(purchase, PurchaseOrder.class); JSONArray array = JSONArray.fromObject(data); List<PurchaseOrderDetail> list = new ArrayList<PurchaseOrderDetail>(); for (int i = 0; i < array.toArray().length; i++) { // 遍历循环,去除最后一项统计栏的信息 JSONObject json = JSONObject.fromObject(array.toArray()[i]); PurchaseOrderDetail resourceBean = (PurchaseOrderDetail) JSONObject.toBean(json, PurchaseOrderDetail.class); if (resourceBean.getGoodsCode().equals("<b>统计:</b>") == false) { list.add(resourceBean); } } return purchaseOrderService.editPurchaseOrder(order, list); }
/** * 将一个页面挂件json参数格式转为Map格式 * * @param paramJson * @return */ public static List<Map<String, String>> jsonToMapList(String paramJson) { JSONArray tempArray = JSONArray.fromObject(paramJson); List<Map<String, String>> paramList = new ArrayList<Map<String, String>>(); Object[] paramArray = tempArray.toArray(); for (Object param : paramArray) { JSONObject paramObj = JSONObject.fromObject(param); paramList.add((Map<String, String>) JSONObject.toBean(paramObj, Map.class)); } return paramList; }
public void toObject(Reader in, Object target) throws IOException { StringBuilder sb = new StringBuilder(); char[] buffer = new char[1024]; int len = 0; while ((len = in.read(buffer)) > 0) { sb.append(buffer, 0, len); } if (target != null && sb.length() > 0 && sb.charAt(0) == '[') { JSONArray jsonArray = JSONArray.fromObject(sb.toString()); if (target.getClass().isArray()) { JSONArray.toArray(jsonArray, target, new JsonConfig()); } else { JSONArray.toList(jsonArray, target, new JsonConfig()); } } else { JSONObject jsonObject = JSONObject.fromObject(sb.toString()); JSONObject.toBean(jsonObject, target, new JsonConfig()); } }
/** * 从json数组中得到相应java数组 * * @param jsonString * @return */ public static Object[] getObjectArray4Json(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); }