1. 程式人生 > >字串轉json陣列

字串轉json陣列

java     contorller

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

String content = inBankOdd.getContent().replace(""","\"");
      if(!content.equals("") && content != null){
         JSONArray jsonArray = JSONArray.fromObject(content);
         if(jsonArray.size()>0
){ int num = 0 ; double sumI = 0 ; double sumP = 0 ; for(int i=0;i<jsonArray.size();i++){ JSONObject object = jsonArray.getJSONObject(i); // 遍歷 jsonarray 陣列,把每一個物件轉成 json 物件 String id = object.getString("id"); String inDis = object.getString("inDis"
); String number = object.getString("number"); String price = object.getString("price"); String pricing = "0"; String practical = "0"; if(!price.isEmpty() && !number.isEmpty()){ pricing = new BigDecimal(number).multiply(new BigDecimal(price)).toString(); //單價乘數量 碼洋
sumI = new BigDecimal(sumI).add(new BigDecimal(pricing)).doubleValue(); //碼洋累加 } if(!inDis.isEmpty() && !pricing.equals("0")){ practical = new BigDecimal(pricing).multiply(new BigDecimal(inDis)).toString(); //入庫折扣乘碼洋 實洋 sumP = new BigDecimal(sumP).add(new BigDecimal(practical)).doubleValue(); //實洋累加 } else { practical = pricing ; } // String teachingMaterialNumber = teachingMaterial.getNumber().trim(); // if(teachingMaterialNumber != null && teachingMaterialNumber.length() != 0) { // if(!number.isEmpty()){ // teachingMaterial.setNumber(String.valueOf((Integer.valueOf(number)+Integer.valueOf(teachingMaterial.getNumber())))); // num += Integer.valueOf(number); //數量累加 // } // } else { // teachingMaterial.setNumber(number); // } } inBankOdd.setContent(content); inBankOdd.setNumber(String.valueOf(num)); inBankOdd.setPricing(String.valueOf(sumI)); inBankOdd.setPractical(String.valueOf(sumP)); } }