MagicalRecord importFromObject:带字典的JSON?

我正在解析一些以这种格式出现的JSON:

{ dataId = "823o7tr23d387g"; category = "link"; details = { text = "Some text associated with the link"; url = "http://www.url.com"; thumbnail_url = "http://sofzh.miximages.com/objective-c/picture.jpeg"; }; source = "CNN"; }, { ... }, 

MagicalRecord有一个很棒的导入方法: + (id) importFromObject:(id)data; 但它是否支持JSON字典中的字典,以便它可以自动将details {}映射到适当的属性?

是否有命名约定或我需要使用的东西?

查看本文关于使用MagicalRecord自动导入JSON,特别是数据密钥路径支持部分

http://www.cimgf.com/2012/05/29/importing-data-made-easy/

数据密钥支持

键值编码是Objective C中常用且有效的工具.MagicalImport允许您将keyPaths指定为mappedKeyName的一部分,从而使您可以访问其中的一些function。 如果您熟悉KVC,这应该是一个相当简单的function,因为Magicalmport将这些指定的密钥传递给了KVC方法。 Keypath支持允许您将数据映射到可能与数据模型不具有完全相同层次结构的实体。 例如,数据实体可以存储纬度和经度,但源数据看起来更像是:

 { "name": "Point Of Origin", "location": { "latitude": 0.00, "longitude": 0.00 } } 

在这种情况下,我们可以在mappedKeyName配置中指定数据导入关键路径,location.latitude和location.longitude,以挖掘嵌套数据结构并将这些值专门导入我们的核心数据实体。

Scott提到的博客是使用MagicalRecord的必读书。

此外,如果默认的+ (id) importFromObject:(id)data对某些NSDictionary数据不起作用,则可以始终覆盖NSManagedObject子类中的- (BOOL) importValuesForKeysWithObject:(id)objectData方法以实现精确控制映射。

这是我最近的一个项目的片段:

 // override MagicalRecord's implementation with additional set up for Dialogue relationship - (BOOL) importValuesForKeysWithObject:(id)objectData { BOOL result = [super importValuesForKeysWithObject:objectData]; // update lesson-dialogue data id dialogueDicts = [objectData objectForKey:@"dialogue"]; if ([dialogueDicts isKindOfClass:[NSArray class]]) { for (id dialogueDict in dialogueDicts) { DialogueSentence *dialogue = [DialogueSentence findFirstByAttribute:@"id" withValue:[[dialogueDict objectForKey:@"id"]]; if (dialogue == nil) { dialogue = [DialogueSentence createEntity]; } [dialogue importValuesForKeysWithObject:dialogueDict]; [self addDialoguesObject:dialogue]; // connect the relationship } } return result; } 

顺便说一句,您可能希望创建NSManagedObject子类的类别并在那里编写重写代码,因为当您升级Core Data模型版本并重新生成NSManagedObject子类时,您自己的代码将不会被删除。