在iPhone上存储json数据:保存json字符串,因为它是从json创建一个对象并使用NSCoding + NSKeyedArchiver

时间:2022-02-28 16:58:08

In my iPhone application I get json data from a remote server, parse it using the Json Framework and present it in a UIview. Also want to be able to give the user the option to store the data on the device so that it can also be viewed offline. I was wondering if storing the json data directly is a better or worse choice than making an object and then saving it using NSCoding + NSKeyedArchiver. I assume that the advantage of storing a json string as it is, is that it takes less space on disc than an archived object while, on the other hand, by storing archived objects you don't have to parse the stored data every time thus using less memory.

在我的iPhone应用程序中,我从远程服务器获取json数据,使用Json Framework解析它并将其呈现在UIview中。还希望能够为用户提供将数据存储在设备上的选项,以便也可以脱机查看。我想知道是否直接存储json数据比制作一个对象更好或更差,然后使用NSCoding + NSKeyedArchiver保存它。我认为存储json字符串的优点是,它在光盘上占用的空间比存档对象少,而另一方面,通过存储存档对象,您不必每次都解析存储的数据使用更少的内存。

Is there a best overall choice? Are there any best practices on that matter? The json files size is approximately 8KB.

有最好的整体选择吗?在这个问题上有没有最佳实践? json文件大小约为8KB。

1 个解决方案

#1


6  

I use a different approach. Once the JSON data is parsed in my app, its stored in a NSDictionary. I persist that as a .plist file.

我使用不同的方法。一旦在我的应用程序中解析了JSON数据,它就存储在NSDictionary中。我坚持认为它是一个.plist文件。

[myDictionary writeToFile:[self saveFilePath] atomically:YES];

To Load:

NSMutableDictionary *wholeDictionary = [NSDictionary dictionaryWithContentsOfFile:[self saveFilePath]];

That's it!

#1


6  

I use a different approach. Once the JSON data is parsed in my app, its stored in a NSDictionary. I persist that as a .plist file.

我使用不同的方法。一旦在我的应用程序中解析了JSON数据,它就存储在NSDictionary中。我坚持认为它是一个.plist文件。

[myDictionary writeToFile:[self saveFilePath] atomically:YES];

To Load:

NSMutableDictionary *wholeDictionary = [NSDictionary dictionaryWithContentsOfFile:[self saveFilePath]];

That's it!