通过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();
}
}
}