Xml文件和Json文件的转换

旧城等待, 2022-05-24 13:28 247阅读 0赞

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

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

涉及到

xml转换为json

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

jsonobjec和jsonarray的转换

  1. public String getXmlElement(String pduName, String moduleName, String jenkinsJobName, String getType) {
  2. MyConfig myConfig = new MyConfig();
  3. String jenkinsUser = myConfig.getJenkinsUsername();
  4. String jenkinsPass = myConfig.getJenkinsPassword();
  5. String jsonString = null;
  6. String oldchar = "version=\"1.1\"";
  7. String newchar = "version=\"1.0\"";
  8. try {
  9. jsonString = HttpClientGetPost.httpclientGet(jenkinsUser, jenkinsPass, pduName, moduleName, jenkinsJobName, "getXml");
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. String jsonStringnew = jsonString.replace("version='1.1'", "version='1.0'");
  14. XMLSerializer xmlSerializer = new XMLSerializer();
  15. JSON json = xmlSerializer.read(jsonStringnew);
  16. if (getType.equals("gitAddr")) {
  17. net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(json);
  18. net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject1.get("scm"));
  19. net.sf.json.JSONArray jsonArray = jsonObject2.getJSONArray("userRemoteConfigs");
  20. String str = jsonArray.get(0).toString();
  21. String gitAddr = str.substring(0, str.length() - 1);
  22. System.out.println(gitAddr);
  23. return gitAddr;
  24. }
  25. if (getType.equals("mavenOrder")) {
  26. net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(json);
  27. Object goals = jsonObject1.get("goals");
  28. String mavenOrder = goals.toString();
  29. return mavenOrder;
  30. }
  31. if (getType.equals("healthyUrl")) {
  32. String jsonStringnew2 = jsonString.replace(oldchar, newchar);
  33. JSON json2 = xmlSerializer.read(jsonStringnew2);
  34. net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json2);
  35. net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject.get("properties"));
  36. List list = (List) jsonObject2.get("hudson.model.ParametersDefinitionProperty");
  37. JSONObject jsonObject1 = (JSONObject) list.get(0);
  38. JSONObject jsonObject3 = JSONObject.fromObject(jsonObject1.get("hudson.model.TextParameterDefinition"));
  39. String healthUrl = jsonObject3.get("defaultValue").toString();
  40. String healthUrlNew = healthUrl.substring(healthUrl.indexOf(":"));
  41. System.out.println(healthUrlNew);
  42. return healthUrlNew;
  43. }
  44. if (getType.equals("jarAddr")) {
  45. String jsonStringnew2 = jsonString.replace(oldchar, newchar);
  46. JSON json2 = xmlSerializer.read(jsonStringnew2);
  47. net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json2);
  48. net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject.get("builders"));
  49. net.sf.json.JSONObject jsonObject3 = JSONObject.fromObject(jsonObject2.get("hudson.plugins.copyartifact.CopyArtifact"));
  50. String jarAddr = jsonObject3.get("filter").toString();
  51. System.out.println(jarAddr);
  52. return jarAddr;
  53. }
  54. if (getType.equals("grayexpDeploy")) {
  55. String jsonStringnew2 = jsonString.replace(oldchar, newchar);
  56. JSON json2 = xmlSerializer.read(jsonStringnew2);
  57. net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json2);
  58. net.sf.json.JSONObject jsonObject2 = JSONObject.fromObject(jsonObject.get("builders"));
  59. net.sf.json.JSONArray jsonArray = jsonObject2.getJSONArray("hudson.plugins.copyartifact.CopyArtifact");
  60. net.sf.json.JSONObject jsonObject3 = jsonArray.getJSONObject(1);
  61. //获取灰度部署job的jar包信息
  62. String jarAddr = jsonObject3.get("filter").toString();
  63. System.out.println(jarAddr);
  64. return jarAddr;
  65. }
  66. return "";
  67. }

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读