博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Json转list,二层解析转换
阅读量:6075 次
发布时间:2019-06-20

本文共 6427 字,大约阅读时间需要 21 分钟。

一层结构的数据:

{    "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
userInfos = (List
) 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
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
classLists = (List
) 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;    };}

 

转载地址:http://ztxgx.baihongyu.com/

你可能感兴趣的文章
Fix: Windows Script Host access is disabled on this machine
查看>>
Class.forName()用法详解
查看>>
Silverlight 在IIS5.1或6.0下部署
查看>>
Linux内核的同步机制(转)
查看>>
用SQL语句获得一个存储过程返回的表
查看>>
论性能测试
查看>>
(转)记录程序崩溃时的调用堆栈
查看>>
Java NIO缓冲区(Buffer)(3)
查看>>
jdbc连接一些问题和常规操作
查看>>
这些代码都是 吃屁的,
查看>>
bundle id
查看>>
用shell统计表格数据
查看>>
iproute2+tc notes
查看>>
【PythonChallenge】Level 5
查看>>
OpenCV合成视频
查看>>
魔幻般冒泡背景的CSS3按钮动画
查看>>
Leetcode: Median of two sorted Array
查看>>
MySQL常见报错汇总
查看>>
Zuul 网关搭建
查看>>
合并两个有序链表
查看>>