在Android中解析巨大的JSON对象?

发布于 2024-10-30 23:44:35 字数 2365 浏览 3 评论 0原文

Android中如何解析巨大的JSON文件?

当我解析这样的文件时,我在日志上看到类似这样的内容:

04-05 15:55:46.490: DEBUG/dalvikvm(3847): GC freed 12159 objects / 557744 bytes in 142ms
04-05 15:55:46.490: INFO/global(3847): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-05 15:55:46.650: DEBUG/dalvikvm(3847): GC freed 56 objects / 100176 bytes in 115ms
04-05 15:55:46.660: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.207MB for 110626-byte allocation
04-05 15:55:46.760: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 104ms
04-05 15:55:46.890: DEBUG/dalvikvm(3847): GC freed 3 objects / 73856 bytes in 115ms
04-05 15:55:47.050: DEBUG/dalvikvm(3847): GC freed 7 objects / 110872 bytes in 113ms
04-05 15:55:47.050: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.426MB for 248896-byte allocation
04-05 15:55:47.150: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 104ms
04-05 15:55:47.310: DEBUG/dalvikvm(3847): GC freed 8 objects / 166232 bytes in 106ms
04-05 15:55:47.310: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.624MB for 373340-byte allocation
04-05 15:55:47.420: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 106ms
04-05 15:55:47.580: DEBUG/dalvikvm(3847): GC freed 10 objects / 249288 bytes in 111ms
04-05 15:55:47.590: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.920MB for 560006-byte allocation
04-05 15:55:47.690: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 98ms
04-05 15:55:47.810: DEBUG/dalvikvm(3847): GC freed 12 objects / 373792 bytes in 88ms
04-05 15:55:47.910: DEBUG/dalvikvm(3847): GC freed 4 objects / 560088 bytes in 87ms
04-05 15:55:48.010: DEBUG/dalvikvm(3847): GC freed 5 objects / 128 bytes in 88ms
04-05 15:55:48.010: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 5.350MB for 461302-byte allocation
04-05 15:55:48.130: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 120ms
04-05 15:55:48.990: DEBUG/dalvikvm(3847): GC freed 10419 objects / 1383992 bytes in 109ms
04-05 15:55:49.720: DEBUG/dalvikvm(3847): GC freed 8769 objects / 384360 bytes in 115ms
04-05 15:55:50.550: DEBUG/dalvikvm(3847): GC freed 10472 objects / 454880 bytes in 129ms
04-05 15:55:51.590: DEBUG/dalvikvm(3847): GC freed 12663 objects / 554440 bytes in 147ms

Is there a pull parser for Json files?遇到这样的事情该怎么处理呢?

How to parse huge JSON file in Android?

When I parse such file I see on something like this on logs:

04-05 15:55:46.490: DEBUG/dalvikvm(3847): GC freed 12159 objects / 557744 bytes in 142ms
04-05 15:55:46.490: INFO/global(3847): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-05 15:55:46.650: DEBUG/dalvikvm(3847): GC freed 56 objects / 100176 bytes in 115ms
04-05 15:55:46.660: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.207MB for 110626-byte allocation
04-05 15:55:46.760: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 104ms
04-05 15:55:46.890: DEBUG/dalvikvm(3847): GC freed 3 objects / 73856 bytes in 115ms
04-05 15:55:47.050: DEBUG/dalvikvm(3847): GC freed 7 objects / 110872 bytes in 113ms
04-05 15:55:47.050: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.426MB for 248896-byte allocation
04-05 15:55:47.150: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 104ms
04-05 15:55:47.310: DEBUG/dalvikvm(3847): GC freed 8 objects / 166232 bytes in 106ms
04-05 15:55:47.310: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.624MB for 373340-byte allocation
04-05 15:55:47.420: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 106ms
04-05 15:55:47.580: DEBUG/dalvikvm(3847): GC freed 10 objects / 249288 bytes in 111ms
04-05 15:55:47.590: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 4.920MB for 560006-byte allocation
04-05 15:55:47.690: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 98ms
04-05 15:55:47.810: DEBUG/dalvikvm(3847): GC freed 12 objects / 373792 bytes in 88ms
04-05 15:55:47.910: DEBUG/dalvikvm(3847): GC freed 4 objects / 560088 bytes in 87ms
04-05 15:55:48.010: DEBUG/dalvikvm(3847): GC freed 5 objects / 128 bytes in 88ms
04-05 15:55:48.010: INFO/dalvikvm-heap(3847): Grow heap (frag case) to 5.350MB for 461302-byte allocation
04-05 15:55:48.130: DEBUG/dalvikvm(3847): GC freed 0 objects / 0 bytes in 120ms
04-05 15:55:48.990: DEBUG/dalvikvm(3847): GC freed 10419 objects / 1383992 bytes in 109ms
04-05 15:55:49.720: DEBUG/dalvikvm(3847): GC freed 8769 objects / 384360 bytes in 115ms
04-05 15:55:50.550: DEBUG/dalvikvm(3847): GC freed 10472 objects / 454880 bytes in 129ms
04-05 15:55:51.590: DEBUG/dalvikvm(3847): GC freed 12663 objects / 554440 bytes in 147ms

Is there a pull parser for Json files? How to deal with such thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剑心龙吟 2024-11-06 23:44:35

看一下 Jackson JSON 解析器,它比 Android 中的解析器更快,也比 GSON 更快,并且支持流式传输。

http://jackson.codehaus.org/

不过,我确实重申了其他人提出的观点,即您的 JSON 很大,这确实表明非最佳设计。例如,如果连接不稳定并且失败几次,您可能会导致您的用户在移动数据计划上产生额外的数据费用。最好尽可能将其分解,这样失败就不会涉及再次请求全部内容。

Take a look at the Jackson JSON parser, it's faster than the one in Android, and faster than GSON, and supports streaming.

http://jackson.codehaus.org/

I do however reiterate the point made by others that your JSON is HUGE and that does suggest a non-optimal design. e.g. if the connection is wonky and it fails a few times you could cause your user to incur additional data costs on a mobile data plan. Always better to break it down where possible so failure doesn't involve requesting the whole lot again.

你的往事 2024-11-06 23:44:35

您可以查看 JsonStreamParsergson 中的 a>一切立即。

You might look into JsonStreamParser in gson if you are looking for an interface that streams json objects instead of parsing everything immediately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文