将 10 MB JSON 响应转换为 NSDictionary 的最有效方法是什么?

发布于 2024-10-06 07:46:13 字数 1088 浏览 4 评论 0原文

我们的应用程序必须以最少的远程 http 请求显示大量数据,因此我们在后端添加了一个端点,以单个 json 响应的形式提供所有必要的数据。这会产生约 1.5MB(压缩后)或大约 8MB 的未压缩 json 格式文本。

没什么大问题,它会在 10 - 30 秒内下载,我们使用 ASIHTTPRequest 将整个响应写入磁盘。

现在到了有趣的部分 - 将未压缩的文件读入内存映射字符串后,我们使用 stig 的 json-framework 将其转换为 NSDictionary。这对于我们应用程序的其余部分以及我们其余 API 端点的典型 2 KB json 响应非常有效。然而,反序列化这 8 MB 的数据需要几秒钟(模拟器)到几分钟(3G 和第二代 iPod Touch)。

我正在研究读取所有这些数据的最佳方法。

我很想使用直接从后端提供的二进制 plist,但我们正在使用 Java,而且我还没有找到合适的库我们的要求,并且在如此紧迫的期限内,编写我们自己的可能不是最好的主意。

如果有任何帮助的话,我们正在解析的 json 字符串主要是 X 项的数组,如下所示:

{
    "items": [ { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" }
             ]
}

将这个 8 MB json 格式的字符串读入内存中的 NSDictionary 的最有效方法是什么?

Our app must display a big chunk of data with minimal remote http requests, so we have added an endpoint to our backend that provides all the necessary data as a single json response. This results in ~1.5MB (compressed) or roughly 8 MBs of uncompressed json-formatted text.

Not much of a problem, it downloads in 10 - 30 seconds and we're using ASIHTTPRequest to write the whole response to disk.

Now comes the fun part - after reading the uncompressed file into a memory mapped string, we use stig's json-framework to convert it into an NSDictionary. This has worked very well for the rest of our app and the typical 2 KB json response for the rest of our API endpoints. However, deserializing these 8 MBs of data takes from a couple of seconds (simulator) to minutes (3G and 2nd gen iPod Touch).

I'm researching the best approach to read in all this data.

I would love to use binary plists served straight from the backend, but we are using Java and I haven't found a proper library that fits our requirements, and with such a tight deadline, writing our own might not be the best idea.

If it helps in any way, the json string we are parsing is mostly an array of X items, like so:

{
    "items": [ { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" },
               { "key1": "value1", "key2": "value2" }
             ]
}

What is the most efficient method to read in this 8 MB json formatted string into a NSDictionary in memory?

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

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

发布评论

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

评论(2

一影成城 2024-10-13 07:46:13

看一下 JSONKit。它非常快,如果你的 JSON 有很多重复的键和值(这很常见),它的“最近实例化的对象缓存”将大大减少用于“最终”NSDictionary 的内存量。

Take a look at JSONKit. It's very fast, and if your JSON has a lot of keys and values that repeat themselves (as is common), its "recently instantiated object cache" will dramatically cut down on the amount of memory used for the "final" NSDictionary.

半仙 2024-10-13 07:46:13

虽然 JSONKit 比 YAJL 更快,但 YAJL 支持流的解析。您应该能够将其与 ASIHTTPRequest 连接起来,以便在下载 JSON 时解析它。请查看标有“数据可用时的文档样式解析”的部分:

http://gabriel.github。 com/yajl-objc/

While JSONKit is faster than YAJL, YAJL supports parsing of the stream. You should be able to wire it up with ASIHTTPRequest to parse the JSON while it's downloading it. Check out the section labeled "Document style parsing as data becomes available" here:

http://gabriel.github.com/yajl-objc/

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