简体   繁体   English

如何在 Java 中快速读取大型 JSON 文件到 ArrayLists

[英]How to quickly read large JSON file to ArrayLists in Java

I'm trying to read a large set of data structured in the .json file format in Java.我正在尝试在 Java 中读取以 .json 文件格式构建的大量数据。 I am currently using org.json.simple to open the .json files.我目前正在使用 org.json.simple 打开 .json 文件。 I can successfully open the .json file from a local directory and read single elements as so:我可以从本地目录成功打开 .json 文件并读取单个元素,如下所示:

public static void main(String[] args) throws Exception {

        JSONParser parser = new JSONParser();

        Object obj = parser.parse(new FileReader("res/data.json"));

        JSONObject jsonObject = (JSONObject) obj;

        for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();
            System.out.println(jsonObject.get(key));
            JSONArray k = (JSONArray) jsonObject.get(key);
            JSONObject p = (JSONObject) k.get(1);
            JSONArray b = (JSONArray) p.get("corners");
            System.out.println(b);
            JSONArray z = (JSONArray) p.get(1);
            System.out.println(z);
        }
    }

The .json file is of this structure: .json 文件具有以下结构:

{
    "buildings": [
        {
            "height": 5,
            "corners": [
                [
                    [
                        579.0,
                        13.0
                    ]
                ],
                [
                    [
                        538.0,
                        39.0
                    ]
                ],
                [
                    [
                        610.0,
                        12.0
                    ]
                ],
                [
                    [
                        558.0,
                        44.0
                    ]
                ],
                [
                    [
                        583.0,
                        29.0
                    ]
                ],
                [
                    [
                        561.0,
                        24.0
                    ]
                ],
                [
                    [
                        596.0,
                        21.0
                    ]
                ]
            ]
        },

where each object in 'buildings' has a height attribute which is an int and an float array of corners.其中“建筑物”中的每个对象都有一个高度属性,它是一个整数和一个浮点数组。 My question is how I can convert these JSONObjects and JSONArrays into primitive Java datatypes and ArrayLists of such.我的问题是如何将这些 JSONObjects 和 JSONArrays 转换为原始 Java 数据类型和此类的 ArrayLists。 In this case, the desired end result would be an array or ArrayList of integers representing the heights of each instance and an ArrayList of Arrays of floats for the corners.在这种情况下,所需的最终结果将是表示每个实例高度的整数数组或 ArrayList,以及角点的浮点数组 ArrayList。

You can use jackson for JSON parsing to convert to ArrayList.您可以使用 jackson 进行 JSON 解析以转换为 ArrayList。 I have written a sample program with the assumption that the JSON is stored in file named test.json我编写了一个示例程序,假设 JSON 存储在名为 test.json 的文件中

     ObjectMapper mapper=new ObjectMapper();
         ArrayList heightList=new ArrayList<Integer>(); //list of heights
         ArrayList cornerList=new ArrayList<float[]>(); //list of corners
         JsonNode buildings=mapper.readTree(new File("test.json")).get("buildings");
         for (JsonNode item:buildings) {
            heightList.add(item.get("height"));
            cornerList.add(item.get("corners"));
         }

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

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