如何从包含JSON记录数组的JSON文件中获取第一条记录
示例文件:
[
{"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.....
]
同样地,文件中可能有1000多条记录。
我想从此文件中获取第一条记录。
答案 0 :(得分:3)
以下代码不会将整个文件作为String加载到内存中。虽然,整个阵列都在内存中。例如,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,即使是第一种方法也可能不是很好。
第三种选择最适合大数据。一次迭代并读取一行,并随时停止。
True Streaming
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);
答案 1 :(得分: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;
}