JSON数据如何在JSONArray中处理

时间:2016-11-21 04:33:14

标签: java json eclipse

我正在做一个简单的java客户端程序,它可以使用eclipse读取url的json数据,我已经在我的本地PC中部署了所有东西,其中一台服务器在VMware中运行,我的VM中没有运行任何Web服务器我的VM中的服务器可以使用http://ServerURL:PortNumber/Anything/etc检索json数据,但是没有从该url返回的所有JSON数据都是我想要的数据,请参考下面我需要的数据来自URL,我想要的数据是< strong> scan_all_result_i ,我需要 scan_all_result_i 进行一些比较

{  
   "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");
             }

1 个答案:

答案 0 :(得分: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的{​​{1}}包含复杂的scan_results,而非数组。因为标记JSONObject包含scan_results(对象表示法)而不是{(数组表示法)。

所以,替换此代码

[

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