您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何將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; }; }
關于如何將Json轉換為list二層解析就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。