Json字符的处理

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值