2016-07-29 78 views
-1

如何從一個JSON文件,其中包含JSON記錄的數組的第一個記錄閱讀從Json的記錄陣列的第一條記錄在一個JSON文件中JAVA

示例文件:

[ 
{"l7ProtocolID":"dhcp","packets_out":1,"bytes_out":400,"start_time":1454281199898,"flow_sample":0,"duration":102,"path":["base","ip","udp","dhcp"],"bytes_in":1200,"l4":[{"client":"68","server":"67","level":0}],"l2":[{"client":"52:54:00:50:04:B2","server":"FF:FF:FF:FF:FF:FF","level":0}],"l3":[{"client":"::ffff:0.0.0.0","server":"::ffff:255.255.255.255","level":0}],"flow_id":"81454281200000731489","applicationID":"dhcp","packets_in":1} 
{"l7ProtocolID":"dhcp","packets_out":1,"bytes_out":400,"start_time":1454281199898,"flow_sample":0,"duration":102,"path":["base","ip","udp","dhcp"],"bytes_in":1200,"l4":[{"client":"68","server":"67","level":0}],"l2":[{"client":"52:54:00:50:04:B2","server":"FF:FF:FF:FF:FF:FF","level":0}],"l3":[{"client":"::ffff:0.0.0.0","server":"::ffff:255.255.255.255","level":0}],"flow_id":"81454281200000731489","applicationID":"dhcp","packets_in":1} 
Record n..... 
] 

而且simillarly文件中可能有1000多條記錄。

我想從該文件中提取第一條記錄。

+0

請仔細閱讀[問]得到了解決。有一百多種不同的方式可以回答這個問題,儘管你有一個答案你不應該有。將來你會做得更好,向我們展示你已經做了什麼來解決這個問題,並且詢問關於你被困住的位的具體問題。我們不是在這裏爲你寫代碼,這是你問的。 –

+0

瞭解了,補充回答。我被卡住的地方是我從錯誤地得到了來自拋棄庫的org.json.simple和JSONObject的JSONArray。我會照顧發佈相關問題@Engineer Dollery。 –

回答

0

使用org.JSON.simple庫

public String ReadJsonFromFile(String fileName){ 
    JSONParser parser = new JSONParser(); 
    String firstRecord = null; 
    try { 
     JSONArray jsonArray = (JSONArray) parser.parse(new FileReader(fileName)); 
     JSONObject jsonObject = (JSONObject) jsonArray.get(0); 
     firstRecord = jsonObject.toString(); 

    } catch (FileNotFoundException e) { 
     LOG.info("JSON -> Can't read from file: File Not Found"); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     LOG.info("JSON -> Can't read File : IO Exception"); 
     e.printStackTrace(); 
    } catch (ParseException e) { 
     LOG.info("JSON -> Can't Parse JSON in File"); 
     e.printStackTrace(); 
    } 

    return firstRecord; 

} 
3

以下代碼不會將整個文件作爲內存中的字符串加載。雖然,整個陣列將在內存中。例如,Gson一次將約10KB的文件字節加載到緩衝區中,並解析每行並添加到數組中。但是,所有1000個對象都將位於數組的堆中。

部分流適用於大多數情況下

public static void readDom() { 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(new FileReader(file)); 
     Gson gson = new GsonBuilder().create(); 
     Person[] people = gson.fromJson(reader, Person[].class); 

     System.out.println("Object mode: " + people[0]); 

    } catch (FileNotFoundException ex) { 
     ... 
    } finally { 
     ... 
    } 
} 

以上代碼的效率比下面:

一杆讀(僅適用於小文件)

String fileContents = FileUtils.readAsString(file); 
Person[] persons = gson.fromJson(fileContents, Person[].class); 

第一一次最多可處理5k-10k行的方法。但是,超過10K,甚至第一種方法可能不是很好。

這第三個選項是最適合大數據的選項。迭代並一次讀取一行,並隨時停止。

真實串流

public static void readStream() { 
    try { 
     JsonReader reader = new JsonReader(new InputStreamReader(stream, "UTF-8")); 
     Gson gson = new GsonBuilder().create(); 

     // Read file in stream mode 
     reader.beginArray(); 
     while (reader.hasNext()) { 
      // Read data into object model 
      Person person = gson.fromJson(reader, Person.class); 
      if (person.getId() == 0) { 
       System.out.println("Stream mode: " + person); 
      } 
      break; 
     } 
     reader.close(); 
    } catch (UnsupportedEncodingException ex) { 
     ... 
    } catch (IOException ex) { 
     ... 
    } 
} 

來源:Reading JSON as Stream using GSON

使用JSON解析處理不匹配POJO結構

如果你不想把創建匹配的麻煩POJO對象圖結構,您可以指示GSON將每一行視爲一個HashMap。

Type type = new TypeToken<Map<String, Object>>(){}.getType(); 
Map<String, Object> thisRow = gson.fromJson(reader, type); 
+0

嘿,泰迪,答案很好 - 全面,格式良好。但是在我們這裏,我們不喜歡爲他們做別人的工作。我們在這裏幫助,而不是'做'。我們在這裏幫助程序員所遇到的特定問題,而不是爲他們實現他們的算法。所以,大部分問題都應該有代碼。如果他們不這樣做,那麼正確的回答就是詢問OP迄今爲止所做的工作以及爲什麼不能爲他們工作,然後幫助他們正確地獲得他們的解決方案。請閱讀[問]。雖然很好的答案。 –

+0

@EngineerDollery謝謝。我同意你的意見。我認爲這可能對其他人有用,也可能在有人提到jso * .o * g庫之前發佈。無論如何,你是對的。 – Teddy