递归解析json数据,并将数据的路径与对应value存入map中

public static void analysisJson(Object objJson, String path,
            Map<String, String> map) {
        // 如果obj为json数组
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            for (int i = 0; i < objArray.size(); i++) {
                analysisJson(objArray.get(i), path, map);
            }
        } else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            Iterator it = jsonObject.keys();
            while (it.hasNext()) {
                String key = it.next().toString();
                Object object = jsonObject.get(key);
                if (path != "" && path != null && !"".equals(path)) {
                    path += "." + key;
                } else {
                    path += key;
                }
                if (object != null && !JSONNull.getInstance().equals(object)) {
                    // 如果得到的是数组
                    if (object instanceof JSONArray) {
                        JSONArray objArray = (JSONArray) object;
                        analysisJson(objArray, path, map);
                        path = modifyPath(path);
                    } else if (object instanceof JSONObject) {
                        analysisJson((JSONObject) object, path, map);
                        path = modifyPath(path);
                    } else {
                        map.put(path, object.toString());
                        // System.out.println("["+path+"]:"+object.toString()+" ");
                        path = modifyPath(path);

                    }
                } else {
                    map.put(path, null);
                    // System.out.println("["+path+"]:"+"null");
                    path = modifyPath(path);
                }
            }
        }
    }

测试

public static void main(String[] args) {
        String data = "[{'id':73,'applyDate':'2018-03-05','reason':'123','auditState':'待审批','editable':false,'student':{'id':1,'sno':'51164500211','name':'张三','nameEn':null,'birthDate':null,'ethnic':{'id':1,'name':'汉族','code':'01'},'gender':{'id':1,'name':'男','code':'1'},'degreeLevel':{'id':1,'name':'硕士研究生','code':'1'},'grade':0,'supervisor':null,'unit':{'id':61,'division':'院系','divisionCode':'01','school':'传播学院','schoolCode':'0123','schoolType':'教学实体','department':'传播学院院部','departmentCode':'012300','departmentType':'教学虚体','since':'2016','until':null,'source':null,'target':null},'discipline':{'id':148,'category':'文学','categoryCode':'05','major':'中国语言文学','majorCode':'0501','minor':'文艺学','minorCode':'050101','originCode':'','degreeLevel':'12','degreeType':'1','area':'p','nature':'gjbzk','since':'2016','until':null,'source':null,'target':null}},'staff':null,'type':{'major':'休学','majorCode':2,'minor':'休学','minorCode':1},'fileNames':[],'applicantUserName':'admin','applicantName':'超级管理员','cTerm':'1','cEarlyGraduationTerm':null,'cStaff':null,'cTransmitDate':null,'cCategory':null,'cMajor':null,'cMinor':null,'cDepartmentCode':null,'cSchoolCode':null,'cReportId':null,'cReportName':null,'cReportLink':null,'cReportDigest':null}]";
        System.out.println(data);
        JSONObject jsonData = JSONObject.fromObject(data.substring(1,
                data.length() - 1));
        Map<String, String> resMap = new HashMap<String, String>();
        analysisJson(jsonData, "", resMap);
        System.out.println();
        for (String str : resMap.keySet()) {
            System.out.println(str + ": " + resMap.get(str));
        }
    }

print

student.unit.schoolType: 教学实体
student.discipline.until: null
reason: 123
student.sno: 51164500211
student.ethnic.name: 汉族
student.unit.until: null
cStaff: null
student.unit.id: 61
student.discipline.target: null
student.birthDate: null
student.unit.since: 2016
student.unit.source: null
student.gender.id: 1
cEarlyGraduationTerm: null
student.discipline.since: 2016
id: 73
student.gender.code: 1
type.major: 休学
student.degreeLevel.id: 1
type.minor: 休学
student.nameEn: null
applicantName: 超级管理员
cReportDigest: null
cReportId: null
student.unit.divisionCode: 01
student.discipline.major: 中国语言文学
student.discipline.source: null
student.gender.name: 
student.unit.school: 传播学院
student.unit.departmentType: 教学虚体
student.discipline.majorCode: 0501
type.majorCode: 2
student.unit.departmentCode: 012300
student.ethnic.id: 1
type.minorCode: 1
applicantUserName: admin
student.unit.department: 传播学院院部
student.discipline.degreeLevel: 12
cSchoolCode: null
student.unit.schoolCode: 0123
student.discipline.minor: 文艺学
student.discipline.id: 148
student.discipline.degreeType: 1
cMajor: null
cMinor: null
cReportLink: null
student.degreeLevel.code: 1
auditState: 待审批
student.discipline.minorCode: 050101
student.degreeLevel.name: 硕士研究生
cCategory: null
student.unit.division: 院系
student.discipline.category: 文学
student.id: 1
cDepartmentCode: null
student.grade: 0
student.discipline.area: p
student.supervisor: null
student.discipline.nature: gjbzk
editable: false
cTransmitDate: null
staff: null
student.name: 张三
cTerm: 1
student.ethnic.code: 01
student.discipline.originCode: 
student.discipline.categoryCode: 05
student.unit.target: null
applyDate: 2018-03-05
cReportName: null

猜你喜欢

转载自blog.csdn.net/zxiang248/article/details/79621026