将10 MB JSON响应转换为NSDictionary的最有效方法是什么? [英] What's the most efficient way of converting a 10 MB JSON response into an NSDictionary?
问题描述
我们的应用必须以最小的远程http请求显示大量数据,因此我们在后端添加了一个端点,它将所有必要的数据作为单个json响应提供。这导致~1.5MB(压缩)或大约8 MB的未压缩json格式文本。
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.
没有太大的问题,它下载10 - 30秒我们'使用ASIHTTPRequest将整个响应写入磁盘。
Not much of a problem, it downloads in 10 - 30 seconds and we're using ASIHTTPRequest to write the whole response to disk.
现在有趣的部分 - 在将未压缩文件读入内存映射字符串后,我们使用stig的 json-framework 将其转换为NSDictionary。这对我们的应用程序的其余部分以及其他API端点的典型2 KB json响应非常有效。但是,将这8 MB的数据反序列化需要几秒钟(模拟器)到几分钟(3G和第二代iPod Touch)。
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).
我正在研究读取所有这些数据的最佳方法。
我很想使用直接来自后端的二进制plist,但我们正在使用Java和我的避风港找不到符合我们要求的合适的图书馆,并且在如此紧迫的截止日期前,写下我们自己可能不是最好的想法。
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.
如果它有任何帮助,我们正在解析的json字符串主要是一个X项的数组,如下所示:
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" }
]
}
将此8 MB json格式化字符串读入的最有效方法是什么?内存中的NSDictionary?
推荐答案
看看 JSONKit 。它非常快,如果你的JSON有很多重复的键和值(这是常见的),它的最近实例化的对象缓存将大大减少用于最终<$ c $的内存量c> 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
.
这篇关于将10 MB JSON响应转换为NSDictionary的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!