JSON解析-HttpClient

 通过HTTP请求获取JSPN文件,并手动解析

package json;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class jsonurl {
	public static void main(String[] args) throws ParseException {
		// CloseableHttpClient: the method of java sending a http request

		/**
		 * STEP 1.establish a http client
		 */
		CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
		String url = "http://szh026l.apac.bosch.com:8080/daas/computing/query_job_rep.do?page=1&recPerPage=20&sortBy=&order=asc&id=1";

		/**
		 * STEP 2.establish a GET request
		 */
		HttpGet httpGet = new HttpGet(url);
		try {
			System.out.println("Executing request:" + httpGet.getMethod() + ' ' + httpGet.getUri());
		} catch (URISyntaxException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		try {

			/**
			 * STEP 3.gain the response from httpGet
			 */
			// execute the GET request,and return the result
			CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
			// response.getEntity():get hold of the response entity
			System.out.println("gain the response entity:" + response.getEntity());
			System.out.println("gain the inputstream:" + response.getEntity().getContent());
			System.out.println("gain the statuscode:" + response.getCode() + ' ' + response.getReasonPhrase());
			String result = EntityUtils.toString(response.getEntity());
			System.out.println(result);

			/**
			 * STEP 4.parse JSON
			 */
			// when the json has array,firstly exchange the String type json into json
			// object,then use the method getJSONArray() to gain the part of array.
			JSONObject jsonObject = JSONObject.fromObject(result);
			System.out.println(jsonObject.get("result"));

			// gain the array:"data":
			JSONArray dataArray = jsonObject.getJSONArray("data");
			int size = dataArray.size();
			System.out.println("Size of data array: " + size);
			for (int i = 0; i < size; i++) {
				JSONObject jsonObject1 = dataArray.getJSONObject(i);
				System.out.print("[" + i + "]id=" + jsonObject1.get("id"));
				System.out.print(",groupId=" + jsonObject1.get("groupId"));
				System.out.print(",previousId=" + jsonObject1.get("previousId"));
				System.out.print(",priority=" + jsonObject1.get("priority"));
				System.out.print(",jobAlias=" + jsonObject1.get("jobAlias"));
				JSONObject jsonObject2 = jsonObject1.getJSONObject("data");
				// for (int j = 0; j < 2; j++) {
				// JSONObject jsonObject2 = dataArray.getJSONObject(i);
				System.out.print(",fileFullPath=" + jsonObject2.get("fileFullPath"));
				System.out.print(",objectId=" + jsonObject2.get("objectId"));
				// }
				System.out.print(",createDate=" + jsonObject1.get("createDate"));
				System.out.print(",maxRetry=" + jsonObject1.get("maxRetry"));
				System.out.print(",executeTimes=" + jsonObject1.get("executeTimes"));
				System.out.print(",buildId=" + jsonObject1.get("buildId"));
				System.out.print(",status=" + jsonObject1.get("status"));
				System.out.println(",lastModify=" + jsonObject1.get("lastModify"));

			}
			if (response != null)
				response.close();
			if (closeableHttpClient != null)
				closeableHttpClient.close();
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值