2016-11-21 64 views
0

我正在做一個簡單的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"); 
      } 
+0

發表您的整個JSON結構響應 –

+0

@PavneetSingh - 我發表了我的整個JSON數據結構 –

回答

0

如果你仔細閱讀你的例外,你會得到你的錯誤:不是JSONArray

錯誤消息: -

Exception in thread "main" org.json.JSONException: JSONObject["scan_results"] is not a JSONArray. 

背後異常原因: -

"scan_results":{ // this tag-key contains complex JsonObject not JSONArray at any level. 
     "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":"" 
     }, 

異常將提高在該行實際上

JSONArray jarr = jobj.getJSONArray("scan_results"); 

只是監守沒有在您添加到您的問題中的完整json數據中的任何級別的數組。

這裏,tag-key名爲scan_results包含複雜的JSONObject裏面沒有數組。因爲標籤scan_results包含{(對象表示法)而不是[(數組表示法)。

所以,替換此代碼

JSONArray jarr = jobj.getJSONArray("scan_results"); // scan_results contains jsonobject not array. 

JSONObject jarr = jobj.getJSONObjcet("scan_results"); //this might help you out.