天天看点

android开发中Gson解析复杂jsonandroid开发中Gson解析复杂json

android开发中Gson解析复杂json

这段时间在公司做项目时,用到了gson解析比较复杂的json字符串数据,由于小白一个在网上找了各种文章解决这个问题,最后都以失败告终,后来自己琢磨了一番终于搞定了,如有不对的地方请大家多多指正。
           

1.首先准备我要解析的json字符串

"Status": ,
    "Code": null,
    "Message": null,
    "Body": {
        "listUser": [
            {
                "py": "A",
                "childList": [
                    {
                        "UserID": "B9AD0603-62BC-414F-A40C-ACECF64093F9",
                        "UserName": "安卓测试",
                        "PicUrl": "http://suruitest.oss-cn-shenzhen.aliyuncs.com/Surui/%E5%A4%B4%E5%83%8F/%E7%94%B7.jpg",
                        "OrgName": "惠州市中心血站",
                        "DeptID": "30B1AA95-855E-4CB5-A648-8861F793AD1D",
                        "DeptName": "站长办",
                        "PostName": "惠州市中心血站系统管理员",
                        "Sort": "A",
                        "Total": 
                    },
                    {
                        "UserID": "DC9E7F9F-8158-4A9E-B376-A25DE1BAF8D1",
                        "UserName": "ABC",
                        "PicUrl": "http://suruitest.oss-cn-shenzhen.aliyuncs.com/Surui/%E5%A4%B4%E5%83%8F/%E7%94%B7.jpg",
                        "OrgName": "惠州市中心血站",
                        "DeptID": "D4DBE8C8-133F-48DA-8D19-FC3EFADA36C1",
                        "DeptName": "临床用血管理『发血』科",
                        "PostName": "惠州市中心血站站长",
                        "Sort": "A",
                        "Total": 
                    }
                ]
            },
            {
                "py": "H",
                "childList": [
                    {
                        "UserID": "4fb2d232-2def-46b0-ba47-5b3d290b0419",
                        "UserName": "红尘",
                        "PicUrl": "http://suruitest.oss-cn-shenzhen.aliyuncs.com/Surui/%E5%A4%B4%E5%83%8F/%E7%94%B7.jpg",
                        "OrgName": "惠州市中心血站",
                        "DeptID": "D4DBE8C8-133F-48DA-8D19-FC3EFADA36C1",
                        "DeptName": "临床用血管理『发血』科",
                        "PostName": "惠州市中心血站站长",
                        "Sort": "H",
                        "Total": 
                    }
                ]
            },
            {
                "py": "S",
                "childList": [
                    {
                        "UserID": "be371055-d601-4150-b801-d9ee17be1a82",
                        "UserName": "苏瑞",
                        "PicUrl": "http://suruitest.oss-cn-shenzhen.aliyuncs.com/Surui/%E5%A4%B4%E5%83%8F/%E7%94%B7.jpg",
                        "OrgName": "惠州市中心血站",
                        "DeptID": "30B1AA95-855E-4CB5-A648-8861F793AD1D",
                        "DeptName": "站长办",
                        "PostName": null,
                        "Sort": "S",
                        "Total": 
                    }
                ]
            },
            {
                "py": "T",
                "childList": [
                    {
                        "UserID": "6DFD1482-4E42-4F91-BFC3-9A49297F85C4",
                        "UserName": "test",
                        "PicUrl": "http://suruitest.oss-cn-shenzhen.aliyuncs.com/Surui/%E5%A4%B4%E5%83%8F/%E7%94%B7.jpg",
                        "OrgName": "惠州市中心血站",
                        "DeptID": "D4DBE8C8-133F-48DA-8D19-FC3EFADA36C1",
                        "DeptName": "临床用血管理『发血』科",
                        "PostName": null,
                        "Sort": "T",
                        "Total": 
                    }
                ]
            },
            {
                "py": "X",
                "childList": [
                    {
                        "UserID": "7791350F-F277-45A0-B9C2-B5CA514344FE",
                        "UserName": "系统管理员",
                        "PicUrl": "http://suruitest.oss-cn-shenzhen.aliyuncs.com/Surui/%E5%A4%B4%E5%83%8F/%E7%94%B7.jpg",
                        "OrgName": "惠州市中心血站",
                        "DeptID": "30B1AA95-855E-4CB5-A648-8861F793AD1D",
                        "DeptName": "站长办",
                        "PostName": "惠州市中心血站系统管理员",
                        "Sort": "X",
                        "Total": 
                    }
                ]
            }
        ]
    }
}
           

2.将json字符串转换成实体类

有一个比较好用的工具提供给大家

点击这里:Json转javabean实体类

实体类代码如下

注意:java实体类必须写成内部类,而且不能是静态的(切记不然无法解析)
import java.util.List;

/**
 * Created by Yongjie on 2016/7/17.
 */
public class RootBean {
    private int Status;

    private String Code;

    private String Message;

    private Body Body;

    public void setStatus(int Status) {
        this.Status = Status;
    }

    public int getStatus() {
        return this.Status;
    }

    public void setCode(String Code) {
        this.Code = Code;
    }

    public String getCode() {
        return this.Code;
    }

    public void setMessage(String Message) {
        this.Message = Message;
    }

    public String getMessage() {
        return this.Message;
    }

    public void setBody(Body Body) {
        this.Body = Body;
    }

    public Body getBody() {
        return this.Body;
    }

    //--------------------------------------------------------------
    public class ChildList {
        private String UserID;

        private String UserName;

        private String PicUrl;

        private String OrgName;

        private String DeptID;

        private String DeptName;

        private String PostName;

        private String Sort;

        private int Total;

        public void setUserID(String UserID) {
            this.UserID = UserID;
        }

        public String getUserID() {
            return this.UserID;
        }

        public void setUserName(String UserName) {
            this.UserName = UserName;
        }

        public String getUserName() {
            return this.UserName;
        }

        public void setPicUrl(String PicUrl) {
            this.PicUrl = PicUrl;
        }

        public String getPicUrl() {
            return this.PicUrl;
        }

        public void setOrgName(String OrgName) {
            this.OrgName = OrgName;
        }

        public String getOrgName() {
            return this.OrgName;
        }

        public void setDeptID(String DeptID) {
            this.DeptID = DeptID;
        }

        public String getDeptID() {
            return this.DeptID;
        }

        public void setDeptName(String DeptName) {
            this.DeptName = DeptName;
        }

        public String getDeptName() {
            return this.DeptName;
        }

        public void setPostName(String PostName) {
            this.PostName = PostName;
        }

        public String getPostName() {
            return this.PostName;
        }

        public void setSort(String Sort) {
            this.Sort = Sort;
        }

        public String getSort() {
            return this.Sort;
        }

        public void setTotal(int Total) {
            this.Total = Total;
        }

        public int getTotal() {
            return this.Total;
        }

    }

    //--------------------------------------------------------------
    public class ListUser {
        private String py;

        private List<ChildList> childList;

        public void setPy(String py) {
            this.py = py;
        }

        public String getPy() {
            return this.py;
        }

        public void setChildList(List<ChildList> childList) {
            this.childList = childList;
        }

        public List<ChildList> getChildList() {
            return this.childList;
        }

    }

    public class Body {
        private List<ListUser> listUser;

        public void setListUser(List<ListUser> listUser) {
            this.listUser = listUser;
        }

        public List<ListUser> getListUser() {
            return this.listUser;
        }

    }
}
           

3.将gson.jar包导入到libs下

没有gson.jar包的可以在这里下载

4.测试代码

String json = "上面那串json字符串";
        //先new Gson
        Gson gson = new Gson();
        //把json字符串转Bean实例
        RootBean rootBean = gson.fromJson(json, RootBean.class);
        //从实例中取得Body
        RootBean.Body body = rootBean.getBody();
        //再从Body中取大数组listUser
        List<RootBean.ListUser> listUser = body.getListUser();
        //循环遍历取子集合
        for (RootBean.ListUser ls : listUser) {
            List<RootBean.ChildList> childList = ls.getChildList();
            ////循环遍历子集合取其元素
            for (RootBean.ChildList cls : childList) {
                String userName = cls.getUserName();
                String deptName = cls.getDeptName();
                String orgName = cls.getOrgName();
                System.out.println("userName:" + userName);
                System.out.println("deptName:" + deptName);
                System.out.println("orgName:" + orgName);
            }
        }
           

4.运行结果

userName:安卓测试
deptName:站长办
orgName:惠州市中心血站
userName:ABC
deptName:临床用血管理『发血』科
orgName:惠州市中心血站
userName:红尘
deptName:临床用血管理『发血』科
orgName:惠州市中心血站
userName:苏瑞
deptName:站长办
orgName:惠州市中心血站
userName:test
deptName:临床用血管理『发血』科
orgName:惠州市中心血站
userName:系统管理员
deptName:站长办
orgName:惠州市中心血站
           
最后:感谢大家阅读,转载请指明原处。