Xml檔案和Json檔案的轉換
阿新 • • 發佈:2019-02-14
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 ""; }