[英]How to get the data from specific index in json file java
JSONParser
解析给定文件中的所有json对象,但我想解析从第100个 索引开始到文件末尾的json对象。
我可以稍后使用subList
进行此subList
但是如果我的json文件中有1百万个 json对象,我不想解析所有内容,因为效率会降低。
public static void readJsonFile() {
JSONParser parser = new JSONParser();
try {
JSONArray a = (JSONArray) parser.parse(new FileReader("D:\\2018-4-21.json"));
for (Object o : a.subList(100,a.size())) {
JSONObject checkIn = (JSONObject) o;
String userId = (String) checkIn.get("UserID");
System.out.print(userId);
String inout = (String) checkIn.get("INOUT");
System.out.print(" " + inout);
String swippedDateTime = (String) checkIn.get("SwippedDateTime");
System.out.print(" " + swippedDateTime);
System.out.println("");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}
[
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:25"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:36"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:36"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:36"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:38"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:38"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:38"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:39"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:39"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:39"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:42"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:42"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:42"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:42"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:42"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:42"
},
{
"UserID": "2",
"INOUT": null,
"SwippedDateTime": "2018-4-23 22:49"
}
]
定位索引100的唯一方法是解析所有内容直到索引100。
我认为您真正要问的是如何做到这一点而不在内存中创建不必要的对象。
答案实际上还可以帮助您管理具有数百万条记录的文件,而不会耗尽内存:
使用流解析器。
使用流解析器,您将获得解析后的数据,因此您可以快速跳过前X条记录,然后一次开始处理一条记录,因此您不必在内存中保留多条记录。
这意味着您实际上可以解析占用空间非常小的无限大小的文件。
由于您正在使用GSON,这意味着您需要使用JsonReader
而不是JsonParser
。
如果您有1,000,000条记录,则需要考虑内存使用情况。
最有效的方法是手动读取文件的第一部分-如果您已经显示,所有记录的大小都相同,因此您可以简单地使用InputStream.skip()
-当然,如果您的String字段(如UserID
可以是不同的长度,那么它将无法正常工作。
您可以逐字符读取文件,计数(说)逗号以确定何时跳过100条记录。
跳过文件的第一部分后,应使用流解析器读取其余部分。 Gson会这样做: https : //sites.google.com/site/gson/streaming
您还可以使用流解析器来有效地跳过文件的第一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.