3

如何在 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

是否有 Json 文件的拉解析器?如何处理这样的事情?

4

2 回答 2

7

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

http://jackson.codehaus.org/

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

于 2011-04-05T14:37:00.307 回答
3

如果您正在寻找一个流 json 对象而不是立即解析所有内容的接口,您可能会在gson中查看JsonStreamParser 。

于 2011-04-05T14:30:25.333 回答