2016-11-19 27 views
0

我創建了一個包含我的Json數據的java URL類,並且有一些函數可以獲取我的json數據以進行一些數據比較,但我發現它可能不支持JSONObject將數據傳遞到JSONObject。我是否需要在我的情況下使用JSONArray,因爲我的JSON數據也有數組結構?

使用JSONObject以URL格式傳遞Json數據的任何方法?

 try 
      { 
      JSONObject obj = new JSONObject(); 
      obj.readJsonFromUrl(theUrl); 
      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 problem: 
    The method readJsonFromUrl(URL) is undefined for the type JSONObject 



也有一定的警示作用消息爲JSONObject readJsonFromUrl方法

private static JSONObject readJsonFromUrl(URL theUrl) throws IOException, JSONException {

任何人都可以向我解釋JSON數據如何在java中工作?我看到了很多JSON類的Java類,這讓我很困惑,比如JSONObject,JSONArray,JSONValue。我在網上搜索了一些資料,但我也對此不是很清楚,因爲我是很新的JSON數據處理

這是我的樣本JSON數據,我需要的數據scan_result僅

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

    } 
} 
+0

你確定readJsonFromUrl方法裏面有JSONOBject類嗎? – SpringLearner

+0

@SpringLearner - 我嘗試更改getJsonObject,並向我顯示相同的錯誤消息。你會在JSONObject中推薦任何合適的方法嗎? –

+0

你指的是JavaEE7中的'javax.json.JSONObject'嗎? –

回答

0

你不可錯過json在url中,你可以將它傳遞給body。將Json寫入stream body並使用常規java method發佈。 這是你的問題的解釋的oracle community url

需要的罐子可以從here下載。

測試代碼如下:

URL url = new URL("https://graph.facebook.com/search?q=java&type=post"); 
     try (InputStream is = url.openStream(); 
       JsonReader rdr = Json.createReader(is)) { 

      JsonObject obj = rdr.readObject(); 
      JsonArray results = obj.getJsonArray("data"); 
      for (JsonObject result : results.getValuesAs(JsonObject.class)){ 
       System.out.print(result.getJsonObject("from").getString("name")); 
       System.out.print(": "); 
       System.out.println(result.getString("message", "")); 
       System.out.println("-----------"); 
      } 
     } 
0

如前所述here,有很多方法可以解決這個問題。要麼你必須實現讀取,解析操作自己(@Roland Illig公司的回答)

//you have to implement the readJSON method 
InputStream is = new URL(url).openStream(); 
try { 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); 
    String jsonText = readAll(rd); 
    JSONObject json = new JSONObject(jsonText); 
    return json; 
} finally { 
    is.close(); 
} 

或者你可以使用一個圖書館。最知名和最廣泛使用的庫是jacksongson。 大圖是你試圖將你的json對象映射到一個類。

你有你的JSON文件:

{ "id":1, "name":"eirini", "hobbies":["music","philosophy","football"] }

,並表示這個文件,將存儲的值(取決於您使用可能有不同的要求,比如干將庫中的類,setter方法等等。)

public class Person { 
    public int id; 
    public String name; 
    public List<String> hobbies = new ArrayList<String>(); 

    public String toString() { 
     return name +" has the id: " + id + " the following hobbies" + hobbies.get(0) + " " + hobbies.get(2); 
    } 
} 

最後,在你的主要方法:

public static void main(String[] args) throws IOException, ParseException { 
    ObjectMapper mapper = new ObjectMapper(); 
    InputStream input = this.getClass().getResourceAsStream(FILE); //read your file. There are many ways to achieve this. 
    ObjectMapper mapper = new ObjectMapper(); // just need one 
    Person eirini = mapper.readValue(input, Person.class); 
    System.out.println(eirini.toString());