简体   繁体   English

如何使用大量json解析文件?

[英]How to parse file with lot of json?

I need to parse large file with more than one JSON in it. 我需要解析其中包含多个JSON的大型文件。 I didn't find any way how to do it. 我没有找到任何方法。 File looks like a BSON for mongoDB. 文件看起来像mongoDB的BSON。 File example: 文件示例:

{"column" : value, "column_2" : value}
{"column" : valeu, "column_2" : value}
....

You will need to determine where one JSON begins and another ends within the file. 您将需要确定文件中一个JSON的开始位置和另一个结束的位置。 If each JSON is on an individual line, then this is easy, if not: You can loop through looking for the opening and closing braces, locating the points between each JSON. 如果每个JSON都位于单独的行上,那么就很简单,如果不是这样:您可以循环查找左括号和右括号,在每个JSON之间定位点。

    char[] characters;
    int openBraceCount = 0;
    ArrayList<Integer> breakPoints = new ArrayList<>();

    for(int i = 0; i < characters.length; i++) {
        if(characters[i] == '{') {
            openBraceCount++;
        } else if(characters[i] == '}') {
            openBraceCount--;

            if(openBraceCount == 0) {
                breakPoints.add(i + 1);
            }
        }
    }

You can then break the file apart at each break point, and pass the individual JSON's into whatever your favorite JSON library is. 然后,您可以在每个断点处将文件分开,并将各个JSON传递到您喜欢的JSON库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM