首页 > 代码库 > js数组到后台转 list数组

js数组到后台转 list数组

前台的数组格式是:

[{"credit_record_certificate_id":"452","credit_record_type":"294","credit_record_money_sum":"100","credit_record_monthly_amount":"1000","credit_record_actual_money":"1000","credit_record_deadline":"2014-11-24","credit_record_deadline_t":"2014-11-27","credit_record_remark":"备注备注"},{"credit_record_certificate_id":"452","credit_record_type":"298","credit_record_money_sum":"200","credit_record_monthly_amount":"2000","credit_record_actual_money":"2000","credit_record_deadline":"2014-11-26","credit_record_deadline_t":"2014-11-06","credit_record_remark":"备注"}]

数组格式是这样的,怎么转list的呢?下面我直接粘代码吧,能解释就解释一下,毕竟现在是上班时间!

 

    /**     * 根据传入的json数组,返回指定包含类型的集合     *      * @param jsonStr [{id:1,name:gx},{id:2,name:wxm}]     * @param clazz     * @return     * @author xuxiaohua     */    public static <T> List<T> jsonArrayToList(String jsonStr, Class<T> clazz) {        if (jsonStr == null || jsonStr.trim().equals("")) {            return null;        }        List<T> resultList = new ArrayList<T>();        GsonBuilder builder = new GsonBuilder();        builder.registerTypeAdapter(java.lang.Double.class, new DoubleAdapter());        builder.registerTypeAdapter(java.lang.Integer.class, new IntegerAdapter());        builder.registerTypeAdapter(java.math.BigInteger.class, new BigIntegerAdapter());        builder.registerTypeAdapter(java.lang.Long.class, new LongAdapter());        builder.registerTypeAdapter(java.sql.Date.class, new SqlDateAdapter("yyyy-MM-dd"));        builder.registerTypeAdapter(java.sql.Timestamp.class, new SqlTimestampAdapter("yyyy-MM-dd HH:mm:ss"));        builder.registerTypeAdapter(java.math.BigDecimal.class, new BigDecimalAdapter());        Gson gson = builder.create();        //Gson gson = new Gson();        JsonParser jsonParser = new JsonParser();        JsonElement jsonElement = jsonParser.parse(jsonStr); // 将json字符串转换成JsonElement        JsonArray jsonArray = jsonElement.getAsJsonArray(); // 将JsonElement转换成JsonArray        Iterator<JsonElement> it = jsonArray.iterator(); // Iterator处理        while (it.hasNext()) { // 循环            jsonElement = (JsonElement) it.next(); // 提取JsonElement            String menu = jsonElement.toString(); // JsonElement转换成String            if(StringUtil.isBlank(menu)){                menu = null;            }            T bean = gson.fromJson(menu, clazz); // String转化成JavaBean            resultList.add(bean); // 加入List        }        return resultList;    }    

上面代码方法绝对是一个工具方法

jsonArrayToList

,so,你需要建立一个工具类:JsonUtil。

 

好了,现在页面要传值过来了,我要用到这个方法了开始吧!

@Override	@Transactionalpublic String saveXd(String zxRecord){	List<McsCreRevCertificateModelRecordBean> list=  JsonUtil.jsonArrayToList(zxRecord, McsCreRevCertificateModelRecordBean.class);	List<McsCreRevCertificateModelRecordBean> revCertificateModelRecordBeans=new ArrayList<McsCreRevCertificateModelRecordBean>();	for(McsCreRevCertificateModelRecordBean zxif:list){		revCertificateModelRecordBeans.add(zxif);	}		for(int j=0;j<revCertificateModelRecordBeans.size();j++){			mcscrerevcertificatemodelDao.saveRecord(revCertificateModelRecordBeans.get(j));//保存方法,	}	}

  

 

js数组到后台转 list数组