我正在做一個簡單的java客戶端程序,它可以使用eclipse讀取url的json數據,我已經在我的本地PC中部署了一臺運行在VMware中的服務器,我沒有我的虛擬機中運行的任何Web服務器和我的虛擬機中的服務器都可以使用http://ServerURL:PortNumber/Anything/etc來檢索json數據,但是沒有從該URL返回的所有JSON數據都是我想要的數據,請參閱以下我需要的來自URL的數據,數據I要的是scan_all_result_i而已,我需要scan_all_result_i做一些比較
JSON數據處理如何在JSONArray中
{
"data_id":"a71a3c2588c6472bb4daea41a0b58835",
"file_info":{
"display_name":"",
"file_size":242,
"file_type":"Not available",
"file_type_description":"Not available",
"md5":"aa69ba384f22d0dc0551ace2fbb9ad55",
"sha1":"09ceb54e65df3d3086b222e8643acffe451a6e8a",
"sha256":"dcb46d6ae2a187f789c12f19c44bbe4b9a43bd200a3b306d5e9c1fcf811dc430",
"upload_timestamp":"2016-11-18T09:09:08.390Z"
},
"process_info":{
"blocked_reason":"",
"file_type_skipped_scan":false,
"post_processing":{
"actions_failed":"",
"actions_ran":"",
"converted_destination":"",
"converted_to":"",
"copy_move_destination":""
},
"profile":"File scan",
"progress_percentage":100,
"result":"Allowed",
"user_agent":""
},
"scan_results":{
"data_id":"a71a3c2588c6472bb4daea41a0b58835",
"progress_percentage":100,
"scan_all_result_a":"No Threat Detected",
"scan_all_result_i":0,
"scan_details":{
"Ahnlab":{
"def_time":"2016-11-08T15:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":1,
"threat_found":""
},
"Avira":{
"def_time":"2016-11-08T00:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":133,
"threat_found":""
},
"ClamAV":{
"def_time":"2016-11-08T10:28:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":94,
"threat_found":""
},
"ESET":{
"def_time":"2016-11-08T00:00:00.000Z",
"location":"local",
"scan_result_i":0,
"scan_time":38,
"threat_found":""
}
},
"start_time":"2016-11-18T09:09:08.405Z",
"total_avs":4,
"total_time":250
},
"vulnerability_info":{
}
但我讓我的Eclipse編譯器說,scan_results不是JsonArray的錯誤,這是我在我的編譯器得到的錯誤消息
Exception in thread "main" org.json.JSONException: JSONObject["scan_results"] is not a JSONArray.
at org.json.JSONObject.getJSONArray(JSONObject.java:596)
at FetchResult.main(FetchResult.java:39) <br><br> From the Json data, it's a array format of data but I have not idea what is the error message try to told me, I bit new to JSON data as well,please guide me where is my mistake.<br><br>
這裏是我的java源
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class FetchResult {
public static void main(String[] args) throws IOException, JSONException,ProtocolException {
URL theUrl = new URL ("http://192.168.0.25:8008/file/a71a3c2588c6472bb4daea41a0b58835");
HttpURLConnection con = (HttpURLConnection) theUrl.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
if(responseCode == 200)
{
try
{
InputStream is = con.getInputStream();
BufferedReader read = new BufferedReader (new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String data = "" ;
while((data = read.readLine()) != null)
{
buffer.append(data);
}
String JsonData = buffer.toString();
JSONObject jobj = new JSONObject(JsonData);
JSONArray jarr = jobj.getJSONArray("scan_results");
JSONObject Finaljobj = jarr.getJSONObject(0);
int scan_result = Finaljobj.getInt("scan_all_result_i");
if(scan_result == 0)
{
System.out.print("Start to copy file");
}
發表您的整個JSON結構響應 –
@PavneetSingh - 我發表了我的整個JSON數據結構 –