Java中JSON之全

1.           在不知道你要转的对象的类型的时候,用com.alibaba.fastjson.JSON.parse();

       Object parse = com.alibaba.fastjson.JSON.parse(response);
            System.out.println("parse的类型::::::"+parse.getClass());


2.    如果对象为list数组类型[], 用com.alibaba.fastjson.JSON.parseArray();
            com.alibaba.fastjson.JSONArray parseArray = com.alibaba.fastjson.JSON.parseArray(response);

下面出示一段代码:

package com.awspaas.user.apps.hh500.job;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.json.JSONException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.bpms.util.UtilString;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.hh500.util.WebService;

public class SwanInterfaceJob implements Job {
    private static WebService webService = null;

    @Override
    public void execute(JobExecutionContext jec) {
        try {
            getTestDataUser();
            getTestDataKpi();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public String getTestDataUser() throws JSONException {
        String URL = "http://192.168.1.240:8080/honghu/synchronizInterfaceController.do?getOrg";
        webService = new WebService();
        Map<String, Object> userData = webService.request(URL, "{"mainData": "测试组织架构"}");
        JSONArray jsonArray = null;
        if (!UtilString.isEmpty(userData.get("response"))) {
            DBSql.update("DELETE   FROM  BO_EU_ORG_INTERFACE");
            String response = userData.get("response").toString();

            Object parse = com.alibaba.fastjson.JSON.parse(response);
            System.out.println("parse的类型::::::"+parse.getClass());
            System.out.println(parse);
            
//            com.alibaba.fastjson.JSONArray parseArray = com.alibaba.fastjson.JSON.parseArray(response);
//            System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"+parseArray);
            jsonArray = JSONArray.fromObject(parse.toString());
            List<Map<String, Object>> aList = (List<Map<String, Object>>) jsonArray;
            List<BO> bos = new java.util.ArrayList<BO>();
            BO bo = null;
            for (Map<String, Object> RowJson4 : aList) {
                if (RowJson4.get("userMap") != null) {
                    Map<String, Object> map = (Map<String, Object>) RowJson4.get("userMap");
                    if (map.get("id") != null) {
                        for (Map<String, Object> RowJson3 : aList) {
                            if (RowJson3.get("orgType").toString().equals("3") && RowJson4.get("pId").toString().equals(RowJson3.get("id"))) {
                                for (Map<String, Object> RowJson2 : aList) {
                                    if (RowJson2.get("orgType").toString().equals("2") && RowJson3.get("pId").toString().equals(RowJson2.get("id"))) {
                                        for (Map<String, Object> RowJson1 : aList) {
                                            if (RowJson1.get("orgType").toString().equals("1") && RowJson2.get("pId").toString().equals(RowJson1.get("id"))) {
                                                bo = new BO();
                                                bo.set("COMPANY_ID", RowJson1.get("id"));// 公司ID
                                                bo.set("COMPANY_NAME", RowJson1.get("name"));// 公司
                                                bo.set("DEPT_ID", RowJson2.get("id"));// 部门ID
                                                bo.set("DEPT_NAME", RowJson2.get("name"));//
                                                bo.set("POST_ID", RowJson4.get("id"));// 岗位ID
                                                bo.set("POST_NAME", RowJson4.get("name"));//
                                                bo.set("USER_ID", map.get("empNo"));// 人员账号
                                                bo.set("USER_NAME", map.get("name"));
                                                bos.add(bo);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            System.out.println(bos);
            if (bos != null && bos.size() > 0) {
                SDK.getBOAPI().createDataBO("BO_EU_ORG_INTERFACE", bos, UserContext.fromUID("admin"));
            }
        }
        return jsonArray.toString();
    }

    @SuppressWarnings("unchecked")
    public void getTestDataKpi() throws JSONException {
        String URL = "http://192.168.1.240:8080/honghu/synchronizInterfaceController.do?getKpi";
        net.sf.json.JSONArray jsonArray = null;
        webService = new WebService();
        Map<String, Object> userData = webService.request(URL, "{"mainData": "测试指标"}");
        List<BO> bos = new java.util.ArrayList<BO>();
        if (!UtilString.isEmpty(userData.get("response"))) {
            DBSql.update("DELETE   FROM  BO_EU_WEIGHT_INTERFACE");
            String response = userData.get("response").toString();
            Object parse = com.alibaba.fastjson.JSON.parse(response);
            System.out.println("parse的类型::::::"+parse.getClass());
            System.out.println(parse);
            jsonArray = net.sf.json.JSONArray.fromObject(parse);
            System.out.println(jsonArray);
            List<Map<String, Object>> aList = (List<Map<String, Object>>) jsonArray;
            BO bo = null;
            for (Map<String, Object> RowJson4 : aList) 
                if ((!UtilString.isEmpty(RowJson4.get("type"))) && RowJson4.get("type").toString().equals("4")) {
                    for (Map<String, Object> RowJson3 : aList) {
                        if ((!UtilString.isEmpty(RowJson3.get("type"))) && RowJson3.get("type").toString().equals("3")
                                && RowJson4.get("parentId").toString().equals(RowJson3.get("id"))) {
                            for (Map<String, Object> RowJson2 : aList) {
                                if ((!UtilString.isEmpty(RowJson2.get("type"))) && RowJson2.get("type").toString().equals("2")
                                        && RowJson3.get("parentId").toString().equals(RowJson2.get("id"))) {
                                    for (Map<String, Object> RowJson1 : aList) {
                                        if ((!UtilString.isEmpty(RowJson1.get("type"))) && RowJson1.get("type").toString().equals("1")
                                                && RowJson2.get("parentId").toString().equals(RowJson1.get("id"))) {
                                            bo = new BO();
                                            bo.set("WID", RowJson4.get("id"));// 指标ID
                                            // bo.set("FIRSTTYPE", RowJson1.get("firstType"));//一级指标
                                            bo.set("FIRSTTYPE", RowJson1.get("projectName"));// 一级指标
                                            bo.set("FIRST_CODE", RowJson1.get("projectCode"));
                                            // bo.set("MAXTYPE", RowJson2.get("maxType"));//二级指标
                                            bo.set("MAXTYPE", RowJson2.get("projectName"));// 二级指标
                                            bo.set("MAXTCODE", RowJson2.get("projectCode"));
                                            // bo.set("MINTYPE", RowJson3.get("minType"));//三级指标
                                            bo.set("MINTYPE", RowJson3.get("projectName"));// 三级指标
                                            bo.set("MINCODE", RowJson3.get("projectCode"));
                                            bo.set("PROJECTNAME", RowJson4.get("projectName"));// 四级指标
                                            bo.set("PROJECTCODE", RowJson4.get("projectCode"));
                                            bo.set("NO", RowJson4.get("no"));// 排序序号
                                            bo.set("REMAK", RowJson4.get("remak"));// 备注
                                            bo.set("AFFILIATIONDPTNAME", RowJson4.get("affiliationDptName"));// 归属部门
                                            bo.set("DUTYDPTNAME", RowJson4.get("dutyDptName"));// 责任部门
                                            bos.add(bo);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            System.out.println(bos);
            if (bos != null && bos.size() > 0) {
                SDK.getBOAPI().createDataBO("BO_EU_WEIGHT_INTERFACE", bos, UserContext.fromUID("admin"));
            }
        }

}
原文地址:https://www.cnblogs.com/renpei/p/9857257.html