时间:2021-05-20
一层结构的数据:
{ "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; };}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这篇文章主要介绍了json字符串对象转换代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下1、将字符串转
交换机二层和三层区别是:二层交换机工作于OSI模型的第2层(数据链路层),故而称为二层交换机。二层交换技术是发展比较成熟,二层交换机属数据链路层设备,可以识别数
本文实例讲述了phparray转json及java转换json数据格式操作。分享给大家供大家参考,具体如下:phparray转json数据$arr=array(
二层交换机是指只支持OSI第二层(数据链路层)交换技术的交换机,二层交换技术发展比较成熟,二层交换机属数据链路层设备,可以识别数据包中的MAC地址信息,根据MA
本文实例讲述了C#实现DataTable,List和Json转换的方法。分享给大家供大家参考,具体如下:1.将DataTable或Ilist转换成JSON格式u