json转换实体bean、数组list

json转实体bean

data=[{"xuHao":"201407140001","scaleNo":"01","qrCode":"440100100002B993","beforeWeight":15.85,"fillingWeight":30.50,"steelno":"013267","fillingWorkerID":"","fillingDate":"2014-07-14 09:29:01"},{"xuHao":"201507140002","scaleNo":"01","qrCode":"4401001000013475","beforeWeight":15.85,"fillingWeight":30.50,"steelno":"003212","fillingWorkerID":"","fillingDate":"2015-07-14 09:33:01"}]

 

String strObject=request.getParameter("data");

FillingDetailRecordsEntity requestBean=(FillingDetailRecordsEntity)jsonToObject(strObject,FillingDetailRecordsEntity.class);

public static <T> T jsonToObject(String jsonString, Class<T> pojoCalss) {
try{
      Object pojo;
      net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonString);
      pojo = net.sf.json.JSONObject.toBean(jsonObject, pojoCalss);
      return (T)pojo;
    }catch(Exception ex){
               ex.printStackTrace();
               return null;
    }
}

 

json转实体数组list

JSONArray jsonArray = JSONArray.fromObject(strObject);
List<FillingDetailRecordsEntity> list = JSONArray.toList(jsonArray, FillingDetailRecordsEntity.class);

 

posted @ 2015-07-14 15:08  Kevin_Zhou_9  阅读(1880)  评论(0编辑  收藏  举报