2016-11-20 74 views
1

我正在开发一个java程序,它可以从我的JSON数据结构中从Web URL中检索某些数据。如何使用JsonReader使用JSONObject和JsonArray之间的URL和关系

这里是我的JSON数据:

{ 
    "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":{ 

    } 
} 

但唯一的数据,我想是scan_all_result_i属于scan_results数组,但我遇到的一些it.Ii搜索问题在甲骨文网站上的一些信息和它的显示我的代码可能是正确的(reference 1reference 2

错误仍显示为该部分。我如何使用JsonReader,JSONObjectJSONArray来处理JSON数据。这里的任何人都可以指导我如何进行Json数据处理?我已经搞清楚了这几天,现在没有任何想法。

这里是我的Java源

public class FetchResult { 

    public static void main(String[] args) throws IOException, JSONException { 
     URL theUrl = new URL ("http://192.168.0.25:8008/file/a71a3c2588c6472bb4daea41a0b58835"); 
     HttpURLConnection con = (HttpURLConnection) theUrl.openConnection(); 
     con.setRequestMethod("GET"); 
     int responseCode = con.getResponseCode(); 
     if(responseCode == 200) 
     { 
      try 
      { 
       InputStream is = theUrl.openStream(); 
       JsonReader read = JsonReader.createReader(is); 
       JSONObject obj = read.readObject(); 
       JSONArray result = obj.getJSONArray("scan_results"); 
       For(JSONObject result : result.getValueAs(JSON.Object.class)) 
       { 
        System.out.print(result.getString("scan_all_result_i")); 
       } 
     // JSONObject obj = new JSONObject(); 
     // obj.getJsonObject("scan_results"); 
     // System.out.println(obj.toString()); 
      } 
      catch(MalformedURLException e) 
      { 
       System.out.print("your problem here ...1"); 
      } 
     } 
     else 
     { 
      System.out.print("Can't Connect"); 
     } 
    } 

错误消息,我在日食编译

Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method createReader(InputStream) is undefined for the type JsonReader Type mismatch: cannot convert from JsonObject to JSONObject Syntax error on token "JSONObject", ? expected after this token JSONObject cannot be resolved to a variable The method getValueAs(Class) is undefined for the type JSONArray JSON cannot be resolved to a type Syntax error, insert ";" to complete Statement The method getString(int) in the type JSONArray is not applicable for the arguments (String)

回答

1

在for循环中得到的,它应该是

for (Object obj : result) { 
     JSONObject jsonObj = (JSONObject) obj; 
     System.out.print(jsonObj.getString("scan_all_result_i")); 
    } 
+0

这样我就可以”排除JSONArray以及?只使用JSONObject? – attack

相关问题