如何使用JsonReader处理JsonObject和JsonArray之间的URL和关系

时间:2016-11-20 06:59:33

标签: java json

我正在开发一个java程序,它可以从Web URL检索我的JSON数据结构中的某些数据。

这是我的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数组,但我遇到了一些问题。我在Oracle网站上搜索了一些信息,它显示我的代码可能正确(reference 1reference 2

错误仍然显示在该部分。我如何使用JsonReaderJSONObjectJSONArray处理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");
       }
    }

错误消息,我的eclipse编译器

  

线程中的异常" main" java.lang.Error:未解决的编译问题:           对于JsonReader类型,未定义createReader(InputStream)方法           类型不匹配:无法从JsonObject转换为JSONObject           令牌上的语法错误" JSONObject" ,?预计在这个令牌之后           无法将JSONObject解析为变量           对于JSONArray类型,方法getValueAs(Class)是未定义的           无法将JSON解析为某种类型           语法错误,插入";"完成声明           JSONArray类型中的方法getString(int)不适用于参数(String)

1 个答案:

答案 0 :(得分:1)

在for循环中,它应该是

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