简体   繁体   English

将JSON数据存储到Core Data的最佳方法

[英]Best way to store JSON data in to Core Data

I'm new to Core data and was wondering which was the best way to store a Json Data like this : 我是Core数据的新手,我想知道哪种存储Json Data的最佳方法是这样的:

{"LG" : 
      { "Nexus 5" :
                  { "variant"s : [
                                  {"model" : "Nexus 5 16gb"},
                                  {"model" : "Nexus 5 32gb"}
                                 ]
                  },
      },
      { "Nexus 4" :
                  { "variant"s : [
                                  {"model" : "Nexus 4 16gb"},
                                  {"model" : "Nexus 4 32gb"}
                                 ]
                  },
      },
}, ...... so on..

I tried storing the whole data as a NSDictionary , after making the Attribute Type "Transformable" , but I encountered some errors such as NSUnknownKeyException . 在将Attribute Type "Transformable" ,我尝试将所有数据存储为NSDictionary ,但是遇到了诸如NSUnknownKeyException错误。

I would like to store the data as NSString and make use of some Relationships . 我想将数据存储为NSString并利用一些Relationships

Can someone tell me what might be the best way to store the same and not saving the whole bunch of data as an NSDictionary ? 有人可以告诉我什么是最好的方法,而不是将整个数据保存为NSDictionary吗?

My two cents. 我的两分钱。

Brand <-->> Model <-->> Variant Brand <->> Model <->> Variant

where Brand will contain LG, etc., Model will contain Nexus 4, etc. and Variant will contain Nexus 5 16GB, etc. 其中“ Brand将包含LG等,“ Model将包含Nexus 4等,而“ Variant将包含Nexus 5 16GB等。

Brand has a to many relationships with Model . 品牌与Model有很多关系。 Model has a to many relationship with Variant . ModelVariant有很多关系。

Please refer to my prev answer on how to set up a to many relationship: Setting up a parent-child relationship in Core Data . 请参阅我关于如何建立多对多关系的上一答案: 在Core Data中建立父子关系

Anyway you should Apple doc and google a little bit. 无论如何,您都应该使用Apple Doc和Google。 You can find a lot of materials out of there (eg Core Data from Scratch: Relationships and More Fetching ). 您可以在那里找到很多资料(例如,Scratch的核心数据:关系和更多获取 )。

I'd recommend reading up about Core Data and about relational databases in general, to avoid getting a design that might be absolute rubbish. 我建议阅读一般性的有关Core Data和有关关系数据库的内容,以避免获得可能是绝对垃圾的设计。 The Core Data model should be at the centre of everything. 核心数据模型应该位于一切的中心。 That's what you need to design. 那就是您需要设计的。

How you receive data in JSON is very secondary. 在JSON中接收数据的方式非常次要。 Ignore JSON completely for the moment. 暂时完全忽略JSON。 First you need to get the database design right. 首先,您需要正确进行数据库设计。 Once you've got that, putting the things that are delivered to you through JSON is quite simple. 一旦做到这一点,将通过JSON传递给您的东西放到很简单。

I'd have a table "Variant" which currently just has a variant with a string for the name, like "Nexus 5 16gb". 我要有一个表格“ Variant”,该表格目前只有一个带有名称字符串的变体,例如“ Nexus 5 16gb”。 Then I'd have a table "phone" with a relation to the manufacture, a string for the name, and a one-to-many relation for the "Variant". 然后,我将创建一个表“ phone”,其中包含与制造商的关系,名称的字符串以及“ Variant”的一对多关系。 Then I'd have a table "manufacturer", with a string for the name, and a one-to-many relation for the "Phones". 然后,我将创建一个表“ manufacturer”,其中包含一个字符串作为名称,以及一个“一对多”关系用于“ Phones”。

best way? 最好的办法? don't store json as a string! 不要将json存储为字符串!

translate the json into a valid object graph while parsing 解析时将json转换为有效的对象图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM