Xml文件和Json文件的转换

Xml文件解析麻烦,所以一般将Xml文件转换为Json文件来获取文件中的某个信息。

因为公司要做Jenkins的二次开发,所以会用到获取Jenkins的Job信息功能,Jenkins的job信息是Xml存储的,此时就转换为了Json,进行信息读取,具体代码如下。

涉及到

xml转换为json

 XMLSerializer xmlSerializer = new XMLSerializer();
 JSON json = xmlSerializer.read(jsonStringnew);

jsonobjec和jsonarray的转换

public String getXmlElement(String pduName, String moduleName, String jenkinsJobName, String getType) {
        MyConfig myConfig = new MyConfig();
        String jenkinsUser = myConfig.getJenkinsUsername();
        String jenkinsPass = myConfig.getJenkinsPassword();
        String jsonString = null;
        String oldchar = "version=\"1.1\"";
        String newchar = "version=\"1.0\"";
        try {
            jsonString = HttpClientGetPost.httpclientGet(jenkinsUser, jenkinsPass, pduName, moduleName, jenkinsJobName, "getXml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonStringnew = jsonString.replace("version='1.1'", "version='1.0'");
        XMLSerializer xmlSerializer = new XMLSerializer();
        JSON json = xmlSerializer.read(jsonStringnew);
        if (getType.equals("gitAddr")) {
            net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(json);
            net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject1.get("scm"));
            net.sf.json.JSONArray jsonArray = jsonObject2.getJSONArray("userRemoteConfigs");
            String str = jsonArray.get(0).toString();
            String gitAddr = str.substring(0, str.length() - 1);
            System.out.println(gitAddr);
            return gitAddr;
        }
        if (getType.equals("mavenOrder")) {
            net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(json);
            Object goals = jsonObject1.get("goals");
            String mavenOrder = goals.toString();
            return mavenOrder;
        }
        if (getType.equals("healthyUrl")) {

            String jsonStringnew2 = jsonString.replace(oldchar, newchar);
            JSON json2 = xmlSerializer.read(jsonStringnew2);
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json2);

            net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject.get("properties"));
            List list = (List) jsonObject2.get("hudson.model.ParametersDefinitionProperty");
            JSONObject jsonObject1 = (JSONObject) list.get(0);
            JSONObject jsonObject3 = JSONObject.fromObject(jsonObject1.get("hudson.model.TextParameterDefinition"));
            String healthUrl = jsonObject3.get("defaultValue").toString();
            String healthUrlNew = healthUrl.substring(healthUrl.indexOf(":"));
            System.out.println(healthUrlNew);
            return healthUrlNew;
        }
        if (getType.equals("jarAddr")) {
            String jsonStringnew2 = jsonString.replace(oldchar, newchar);
            JSON json2 = xmlSerializer.read(jsonStringnew2);
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json2);
            net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject.get("builders"));
            net.sf.json.JSONObject jsonObject3 = JSONObject.fromObject(jsonObject2.get("hudson.plugins.copyartifact.CopyArtifact"));
            String jarAddr = jsonObject3.get("filter").toString();
            System.out.println(jarAddr);
            return jarAddr;
        }

        if (getType.equals("grayexpDeploy")) {

            String jsonStringnew2 = jsonString.replace(oldchar, newchar);
            JSON json2 = xmlSerializer.read(jsonStringnew2);
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json2);

            net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject.get("builders"));
            net.sf.json.JSONArray jsonArray = jsonObject2.getJSONArray("hudson.plugins.copyartifact.CopyArtifact");
            net.sf.json.JSONObject jsonObject3 = jsonArray.getJSONObject(1);
            //获取灰度部署job的jar包信息
            String jarAddr = jsonObject3.get("filter").toString();
            System.out.println(jarAddr);
            return jarAddr;
        }
        return "";
    }

猜你喜欢

转载自blog.csdn.net/intelrain/article/details/80481245
今日推荐