java后台转json、转对象、转list集合
前台数据传递到后台转json
1、普通格式转换成对象
String data=request.getParameter("data");
//单数据的时候转换方式
JSONObject json= JSONObject.fromObject(data);
Tree tree = (Tree)JSONObject.toBean(json, Tree.class);
2、Tree格式(多数据)转成对象
HashMap mapClass=new HashMap();
mapClass.put("children", Tree.class); //children属性是一个list集合
Tree tree = (Tree)JSONObject.toBean(json, Tree.class,mapClass); //Tree是对应的实体对象
3、后台将前台传来的一系列的数据转换为list集合
String data = eac.getParamValue("data");
String str = data.replaceAll("\"", "").replaceAll("\\\\", "\"");
//多数据转json的格式
JSONArray json = JSONArray.fromObject(str);
//将json转为list
List<SkdDcdaContractProblemPo> list = (List<SkdDcdaContractProblemPo>) JSONArray.toCollection(json,SkdDcdaContractProblemPo.class);