Json转list,二层解析转换
一层结构的数据:
{
"code": "0",
"results": {
"boyTotal": 0,
"classNumber": 0,
"enrollmentsTotal": 0,
"girlTotal": 0,
"mobile": "15969538305",
"studentTotal": 0,
"userCode": "141920370",
"userName": "杜文兵",
"userType": "1"
}
}
二层结构的数据:
{
"code": "0",
"results": {
"arrayList": [
{
"boyNumber": 61,
"classId": "159",
"className": "17建筑工程技术01",
"deptName": "建筑工程学院",
"enrollments": 61,
"girlNumber": 0,
"majorName": "建筑工程技术",
"studentNumber": 61,
"year": "2017"
},
{
"boyNumber": 55,
"classId": "208",
"className": "17建筑工程技术02",
"deptName": "建筑工程学院",
"enrollments": 56,
"girlNumber": 1,
"majorName": "建筑工程技术",
"studentNumber": 56,
"year": "2017"
},
{
"boyNumber": 56,
"classId": "209",
"className": "17建筑工程技术03",
"deptName": "建筑工程学院",
"enrollments": 56,
"girlNumber": 0,
"majorName": "建筑工程技术",
"studentNumber": 56,
"year": "2017"
},
{
"boyNumber": 4,
"classId": "237",
"className": "17学前教育08",
"deptName": "人文学院",
"enrollments": 67,
"girlNumber": 63,
"majorName": "学前教育",
"studentNumber": 67,
"year": "2017"
},
{
"boyNumber": 4,
"classId": "238",
"className": "17学前教育09",
"deptName": "人文学院",
"enrollments": 67,
"girlNumber": 63,
"majorName": "学前教育",
"studentNumber": 67,
"year": "2017"
},
{
"boyNumber": 5,
"classId": "239",
"className": "17学前教育10",
"deptName": "人文学院",
"enrollments": 67,
"girlNumber": 62,
"majorName": "学前教育",
"studentNumber": 67,
"year": "2017"
}
],
"boyTotal": 185,
"classNumber": 6,
"enrollmentsTotal": 374,
"girlTotal": 189,
"studentTotal": 374,
"userCode": "131530220"
}
}
先上代码:
JSONObject obj1 = new JSONObject(); obj1.put("mobile", "13008662173"); //查询员工编号 JSONObject userInfo = ToolsWebReq.sendPost(ToolsWebReq.TEACHER_SERVER_BASE_URL + ToolsWebReq.API_QUERYUSERID,obj1); List<UserInfo> userInfos = (List<UserInfo>) JsonUtils.JsonToList(userInfo.get("results"), new UserInfo()); String userCode = ""; if(userInfos != null ){ for (int i = 0; i < userInfos.size(); i++) { UserInfo user = userInfos.get(i); userCode = user.getUserCode(); } } System.out.println("userCode="+userCode); //通过员工编号查询班级数 List<ClassTeacherE> list2 = null; JSONObject obj = new JSONObject(); obj.put("userCode", "131530220"); JSONObject classList = ToolsWebReq.sendPost(ToolsWebReq.TEACHER_SERVER_BASE_URL + ToolsWebReq.API_QUERYCLASSNUM,obj); //解析第一层 JSONObject rs = (JSONObject) classList.get("results"); //解析第二层 JSONArray arrayList = (JSONArray) rs.get("arrayList"); List<UserClassInfo> classLists = (List<UserClassInfo>) JsonUtils.JsonToList(rs.get("arrayList"), new UserClassInfo()); System.out.println("classLists.get(0).getClassName()="+classLists.get(0).getClassName());
方法:
package com.xy.common.util; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; public class JsonUtils { /** * json转对象 * * @param input * @return */ public static List<?> JsonToList(Object object,Object classModel) { JSONArray array = JSONArray.fromObject(object); List<?> list2 = net.sf.json.JSONArray.toList(array,classModel, new JsonConfig());//参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据 return list2; } }
对象:
package com.xy.web.entity; import java.util.Date; import javax.persistence.*; public class UserInfo { private String boyTotal; private String classNumber; private String enrollmentsTotal; private String girlTotal; /** * 联系电话 */ private String mobile; private String studentTotal; /** * 用户账号 */ private String userCode; /** * userName */ private String userName; /** * 用户类型(userType):1-员工,2-学生 */ private String userType; public String getBoyTotal() { return boyTotal; } public void setBoyTotal(String boyTotal) { this.boyTotal = boyTotal; } public String getClassNumber() { return classNumber; } public void setClassNumber(String classNumber) { this.classNumber = classNumber; } public String getEnrollmentsTotal() { return enrollmentsTotal; } public void setEnrollmentsTotal(String enrollmentsTotal) { this.enrollmentsTotal = enrollmentsTotal; } public String getGirlTotal() { return girlTotal; } public void setGirlTotal(String girlTotal) { this.girlTotal = girlTotal; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getStudentTotal() { return studentTotal; } public void setStudentTotal(String studentTotal) { this.studentTotal = studentTotal; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; }; }