1、先通过HttpClient的Get方法获取某个JenkinsJob的json数据
HttpGet request = new HttpGet(uri); // 发送get请求
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
HttpResponse response = httpClient.execute(host, request, localContext);
if(type=="custom"){
String result = EntityUtils.toString(response.getEntity());
return result;
}
2、JSONObject 和 JSONArray 的各种转换
try {
jsonString = HttpClientGetPost.httpclientGet(jenkinsUser, jenkinsPass, pduName, moduleName, jenkinsjobName, "custom");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(jsonString);
// Gson gson=new Gson();
// String str=gson.toJson(jsonString);
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(jsonString);
// String actions = jsonObject.getString("actions");
// JsonArray array=new JsonArray(actions);
net.sf.json.JSONArray jsonArray = jsonObject.getJSONArray("actions");
net.sf.json.JSONObject jsonObject1 = new net.sf.json.JSONObject();
net.sf.json.JSONObject jsonObject2 = new net.sf.json.JSONObject();
net.sf.json.JSONArray jsonArray2 = new net.sf.json.JSONArray();
for (int i = 0; i < jsonArray.size(); i++) {
if (i == 1) {
jsonObject1 = jsonArray.getJSONObject(1);
System.out.println(jsonObject1);
break;
}
}
net.sf.json.JSONArray jsonArray1 = jsonObject1.getJSONArray("parameterDefinitions");
if (getType.equals("getHealthUrl")) {
for (int j = 0; j < jsonArray1.size(); j++) {
jsonObject2 = jsonArray1.getJSONObject(j);
String newchar = jsonObject2.toString();
if (newchar.contains("HEATHURL"))
break;
}
}
if (getType.equals("getEnv")) {
for (int j = 0; j < jsonArray1.size(); j++) {
jsonObject2 = jsonArray1.getJSONObject(j);
String newchar = jsonObject2.toString();
if (newchar.contains("ENV"))
break;
}
}
System.out.println(jsonObject2);
String defaultParameterValue = jsonObject2.getString("defaultParameterValue");
System.out.println(defaultParameterValue);
net.sf.json.JSONObject jsonObject3 = net.sf.json.JSONObject.fromObject(defaultParameterValue);
String getValue = jsonObject3.getString("value");
System.out.println(getValue);
return getValue;