簡體   English   中英

如何從JSON文件Java中的特定索引獲取數據

[英]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();
    }
}

我的Json文件

[
    {
        "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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM