加载JSON文件:java.lang.OutOfMemoryError:Java堆空间 [英] Load JSON file: java.lang.OutOfMemoryError: Java heap space

查看:284
本文介绍了加载JSON文件:java.lang.OutOfMemoryError:Java堆空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我试图读取一个非常大的JSON文件,它给我带来了内存错误,因为我发现的所有内容都直接加载到了数组中,而不是能够逐个对象地访问.

I have a problem that I am trying to read a JSON file that can be very large and it gives me a memory error because everything I find is loaded directly into an array instead of being able to access object by object.

我的代码是:

Object obj = parser.parse(new FileReader("file.json"));

            JSONArray jsonObject = (JSONArray) obj;
            Iterator<JSONObject> iterator = jsonObject.iterator();

            while (iterator.hasNext()) {
               ...
            }

我发现的所有内容都不允许我按需装载这些物品,有人知道这是否可能吗?

Everything I find does not allow me to load the items as I need them, does anyone know if it is possible?

推荐答案

我不知道您要使用哪个库进行JSON解析.以我的经验,我建议您使用 Jackson库完成此任务.它具有流支持来管理文件,并避免将所有数据加载到内存中,然后将它们转换为JSON. Jackson允许您将JSON数据格式转换为POJO.

I don't know which library you are using for JSON parsing. For my experience, I suggest you to use the Jackson library to accomplish this task. It has stream support to manage file and avoid to load all data in memory and then convert them into JSON. Jackson allows you to convert JSON data format into POJO.

如果您需要简短的教程来查看如何使用它,可以按照此链接此链接.

If you need a short tutorial to view how to use it, you can follow this link and this link.

这篇关于加载JSON文件:java.lang.OutOfMemoryError:Java堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆