使用JSON将NSDictionary保存到MySQL [英] Saving an NSDictionary to MySQL with JSON

查看:155
本文介绍了使用JSON将NSDictionary保存到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下 NSDictionary 保存到 MySQL 数据库:

I want to save the following NSDictionary to a MySQL database:

NSDictionary notesDictionary:
object: NSString key:"note"
object: NSArray key:'subNotes"
object: NSString key:"publishDate"

棘手的部分是 NSArray subNotes 只是另一个带有与上面相同键的字典数组。所以一个音符放在一个字典中,而字典又有一个字典数组,而这些字典又有一个字典数组,依此类推(每个音符)有一个子注释,子注释有子注释,等等。)

The tricky part is that the NSArray subNotes is just another array of dictionaries with the same keys as above. So a note is put in a dictionary, which in turn has an array of dictionaries, which in turn has an array of dictionaries, and so on (each note has a subnote, and subnotes have subnotes, and so on).

我是 MySQL 的新手,所以以下是我发现将上述字典存储在数据库中的解决方案:

I'm new to MySQL, so the following is the solution I've found to storing the above dictionary in the database:

NSString *jsonString = [notesDictionary JSONRepresentation];

然后我只是将字符串存储到数据库中,并使用类似的方法检索它。这有效,但我不是重新如果它缩放。如果我有1000个音符,这会导致任何性能挫折,因为整个字典保存为字符串,然后从字符串转换回字典。这是一种好的,快速的,安全的方式吗?或者还有其他我应该研究的内容吗?

Then I just store the string into the database, and retrieve it with similar methods. This works, but I'm not sure if it scales. If I had a 1000 notes, would this cause any performance setbacks, as the entire dictionary is saved as a string, and then later converted from a string back to a dictionary. Is this a good, fast, and secure way? Or is there something else I should be looking into?

推荐答案

JSON是一种非常好的序列化格式。我不担心性能或类似的东西。我唯一担心的是确保将MySQL列设置为采用任意长的字符串。 (不确定如何在MySQL中做到这一点,但它不应该太难。)

JSON is a perfectly good serialization format. I wouldn't worry about performance or anything like that. The only concern I'd have is making sure the MySQL column is set up to take an arbitrarily long string. (Not sure how to do that in MySQL offhand, but it shouldn't be too tough.)

这篇关于使用JSON将NSDictionary保存到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆