我有一个核心数据对象图(由通过多对多关系链接的两个实体组成)。
作为一个相对缺乏经验的iPhone开发人员,我很好奇,是否有人可以推荐一种方法以及适合iPhone的JSON实现,这将使我能够:
将核心数据记录转换为JSON字符串(同时保持实体之间的关系);和
将JSON字符串转换回核心数据对象(再次保留实体之间的关系)。
在这一点上,我没有搜索到教程/代码示例,因此将不胜感激。
我有一个核心数据对象图(由通过多对多关系链接的两个实体组成)。
作为一个相对缺乏经验的iPhone开发人员,我很好奇,是否有人可以推荐一种方法以及适合iPhone的JSON实现,这将使我能够:
将核心数据记录转换为JSON字符串(同时保持实体之间的关系);和
将JSON字符串转换回核心数据对象(再次保留实体之间的关系)。
在这一点上,我没有搜索到教程/代码示例,因此将不胜感激。
Answers:
首先,选择一个要使用的JSON库,我个人喜欢TouchJSON,但那里的其他几个库也相当不错。尽管不是很困难,但复杂的部分是将托管对象转换为适合的结构以进行转换。我写这个真正的快,所以它可能有一个或两个错误:)
您调用的方法是:
- (NSString*)jsonStructureFromManagedObjects:(NSArray*)managedObjects;
- (NSArray*)managedObjectsFromJSONStructure:(NSString*)json withManagedObjectContext:(NSManagedObjectContext*)moc;
并实现如下:
- (NSDictionary*)dataStructureFromManagedObject:(NSManagedObject*)managedObject
{
NSDictionary *attributesByName = [[managedObject entity] attributesByName];
NSDictionary *relationshipsByName = [[managedObject entity] relationshipsByName];
NSMutableDictionary *valuesDictionary = [[managedObject dictionaryWithValuesForKeys:[attributesByName allKeys]] mutableCopy];
[valuesDictionary setObject:[[managedObject entity] name] forKey:@"ManagedObjectName"];
for (NSString *relationshipName in [relationshipsByName allKeys]) {
NSRelationshipDescription *description = [[[managedObject entity] relationshipsByName] objectForKey:relationshipName];
if (![description isToMany]) {
NSManagedObject *relationshipObject = [managedObject valueForKey:relationshipName];
[valuesDictionary setObject:[self dataStructureForManagedObject:relationshipObject] forKey:relationshipName];
continue;
}
NSSet *relationshipObjects = [managedObject objectForKey:relationshipName];
NSMutableArray *relationshipArray = [[NSMutableArray alloc] init];
for (NSManagedObject *relationshipObject in relationshipObjects) {
[relationshipArray addObject:[self dataStructureForManagedObject:relationshipObject]];
}
[valuesDictionary setObject:relationshipArray forKey:relationshipName];
}
return [valuesDictionary autorelease];
}
- (NSArray*)dataStructuresFromManagedObjects:(NSArray*)managedObjects
{
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for (NSManagedObject *managedObject in managedObjects) {
[dataArray addObject:[self dataStructureForManagedObject:managedObject]];
}
return [dataArray autorelease];
}
- (NSString*)jsonStructureFromManagedObjects:(NSArray*)managedObjects
{
NSArray *objectsArray = [self dataStructuresFromManagedObjects:managedObjects];
NSString *jsonString = [[CJSONSerializer serializer] serializeArray:objectsArray];
return jsonString;
}
- (NSManagedObject*)managedObjectFromStructure:(NSDictionary*)structureDictionary withManagedObjectContext:(NSManagedObjectContext*)moc
{
NSString *objectName = [structureDictionary objectForKey:@"ManagedObjectName"];
NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:objectName inManagedObjectContext:moc];
[managedObject setValuesForKeysWithDictionary:structureDictionary];
for (NSString *relationshipName in [[[managedObject entity] relationshipsByName] allKeys]) {
NSRelationshipDescription *description = [relationshipsByName objectForKey:relationshipName];
if (![description isToMany]) {
NSDictionary *childStructureDictionary = [structureDictionary objectForKey:relationshipName];
NSManagedObject *childObject = [self managedObjectFromStructure:childStructureDictionary withManagedObjectContext:moc];
[managedObject setObject:childObject forKey:relationshipName];
continue;
}
NSMutableSet *relationshipSet = [managedObject mutableSetForKey:relationshipName];
NSArray *relationshipArray = [structureDictionary objectForKey:relationshipName];
for (NSDictionary *childStructureDictionary in relationshipArray) {
NSManagedObject *childObject = [self managedObjectFromStructure:childStructureDictionary withManagedObjectContext:moc];
[relationshipSet addObject:childObject];
}
}
return managedObject;
}
- (NSArray*)managedObjectsFromJSONStructure:(NSString*)json withManagedObjectContext:(NSManagedObjectContext*)moc
{
NSError *error = nil;
NSArray *structureArray = [[CJSONDeserializer deserializer] deserializeAsArray:json error:&error];
NSAssert2(error == nil, @"Failed to deserialize\n%@\n%@", [error localizedDescription], json);
NSMutableArray *objectArray = [[NSMutableArray alloc] init];
for (NSDictionary *structureDictionary in structureArray) {
[objectArray addObject:[self managedObjectFromStructure:structureDictionary withManagedObjectContext:moc]];
}
return [objectArray autorelease];
}
现在这是递归的,因此,如果不小心,您可以轻松地转换整个持久性存储。观察您的关系,并确保它们仅“向下”进入对象树,以便仅获得要转换的对象。
我只想指出一个小的错字,导致代码崩溃,希望这可以节省您几分钟的时间。
- (NSArray*)dataStructuresFromManagedObjects:(NSArray*)managedObjects {
NSMutableArray *dataArray = [[NSArray alloc] init];
for (NSManagedObject *managedObject in managedObjects) {
[dataArray addObject:[self dataStructureFromManagedObject:managedObject]];
}
return [dataArray autorelease];
}
的 NSMutableArray *dataArray = [[NSArray alloc] init]; // This should be NSMutableArray
真的应该是 NSMutableArray *dataArray = [[NSMutableArray alloc] init];
就这些。
谢谢
使用Rails同步Core Data是一个详细的演示,其中包括用于将您的Core Data对象与JSON序列化/反序列化的示例代码(对于Core Data部分,跳至幻灯片55)。他的示例代码假设一个没有关系的相当简单的模型,尽管我认为扩展起来非常容易。
该演示文稿还详细介绍了如何使您的Core Data模型与基于REST的Web应用程序保持同步,并提供了指向一些有用的库(包括ObjectiveResource和ASIHTTPRequest)的指针。不确定这是否是您要尝试的方法,但是即使对于Core Data代码也值得一看。
如果您NSDate
在托管对象中有一个,如上面的注释之一所述,则在序列化包含的对象时会遇到问题NSDate
。一个简单的解决JSONDataRepresentation
方法是为NSDate
使用Objective-C类别添加一种方法。
将这两个文件添加到您的项目:
NSdate.h:
#import <Foundation/Foundation.h>
@interface NSDate (jsondatarepresentation)
- (NSData*) JSONDataRepresentation;
@end
NSDate.m:
#import "NSDate.h"
@implementation NSDate (jsondatarepresentation)
- (NSData*) JSONDataRepresentation {
return [[[NSNumber numberWithDouble:[self timeIntervalSince1970]] stringValue] dataUsingEncoding:NSUTF8StringEncoding];
}
@end
我遇到了这篇很好的文章。
http://touchalicious.com/blog/2009/10/25/turn-core-data-models-into-json.html
由于这是递归的,因此多对多关系将不断循环遍历它们自己。为了避免这种情况,我在核心数据模型中为关系的用户信息字典添加了“ isExportable”键。然后,您可以检查此密钥,并选择不使用它就不遍历关系。
if ([property isKindOfClass:[NSRelationshipDescription class]])
{
NSRelationshipDescription *relationshipDescription = (NSRelationshipDescription *)property;
if ([[[relationshipDescription userInfo] objectForKey:@"isExportable"] boolValue] == YES)
{
NSString *name = [relationshipDescription name];
if ([relationshipDescription isToMany])
{
NSMutableArray *arr = [properties valueForKey:name];
if (!arr)
{
arr = [[NSMutableArray alloc] init];
[properties setValue:arr forKey:name];
}
for (NSManagedObject *o in [self mutableSetValueForKey:name])
{
[arr addObject:[o propertiesDictionary]];
}
}
else
{
NSManagedObject *o = [self valueForKey:name];
[properties setValue:[o propertiesDictionary] forKey:name];
}
}
}
}
只是以为id发布了此问题的快速更新。我遵循了Marcus和Brandon的答案,并提出了用于JSON导出的方法(它仍然使用TouchJSON):
- (NSData*)jsonStructureFromManagedObjects:(NSArray*)managedObjects
{
NSArray *objectsArray = [self dataStructuresFromManagedObjects:managedObjects];
NSData *jsonData = [[CJSONSerializer serializer] serializeArray:objectsArray error:nil];
return jsonData;
}
- (NSArray*)dataStructuresFromManagedObjects:(NSArray*)managedObjects
{
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
for (NSManagedObject *managedObject in managedObjects) {
[dataArray addObject:[self dataStructureFromManagedObject:managedObject]];
}
return dataArray;
}
- (NSDictionary*)dataStructureFromManagedObject:(NSManagedObject*)managedObject
{
NSDictionary *attributesByName = [[managedObject entity] attributesByName];
NSDictionary *relationshipsByName = [[managedObject entity] relationshipsByName];
NSMutableDictionary *valuesDictionary = [[managedObject dictionaryWithValuesForKeys:[attributesByName allKeys]] mutableCopy];
[valuesDictionary setObject:[[managedObject entity] name] forKey:@"ManagedObjectName"];
for (NSString *relationshipName in [relationshipsByName allKeys]) {
NSRelationshipDescription *description = [[[managedObject entity] relationshipsByName] objectForKey:relationshipName];
if ([[[description userInfo] objectForKey:@"isExportable"] boolValue] == YES) {
if (![description isToMany]) {
NSManagedObject *relationshipObject = [managedObject valueForKey:relationshipName];
if (relationshipObject) {
[valuesDictionary setObject:[self dataStructureFromManagedObject:relationshipObject] forKey:relationshipName];
}
continue;
}
NSSet *relationshipObjects = [managedObject valueForKey:relationshipName];
NSMutableArray *relationshipArray = [[NSMutableArray alloc] init];
for (NSManagedObject *relationshipObject in relationshipObjects) {
[relationshipArray addObject:[self dataStructureFromManagedObject:relationshipObject]];
}
[valuesDictionary setObject:relationshipArray forKey:relationshipName];
}
}
return valuesDictionary;
}
我无法执行导入,可能与我不确定使用Magical Record的事实有关,所以我只是循环遍历传入的JSON流并手动创建对象...
Marcus S. Zarra启发了我,将递归的想法付诸实践。在此版本中,您无需在CoreData中设置键,就可以将其剪切并粘贴到您的项目中:-)
// MARK: - encoding and decoding CoreData entity to dictionary
func dataStructureFromManagedObject( managedObject:NSManagedObject?, parentEntity: NSEntityDescription? = nil) -> NSMutableDictionary {
if (managedObject != nil) {
var attributesByName: NSDictionary = managedObject!.entity.attributesByName
var relationshipsByName: NSDictionary = managedObject!.entity.relationshipsByName
var valuesImmutableDictionary: NSDictionary = managedObject!.dictionaryWithValuesForKeys( attributesByName.allKeys)
var valuesDictionary: NSMutableDictionary = valuesImmutableDictionary.mutableCopy() as NSMutableDictionary
valuesDictionary.setObject( managedObject!.entity.name!, forKey: "ManagedObjectName")
for relationshipNameObject in relationshipsByName.allKeys {
var relationshipName: NSString = relationshipNameObject as NSString
var relationshipDescription: NSRelationshipDescription? = relationshipsByName.objectForKey( relationshipName) as? NSRelationshipDescription
if !relationshipDescription!.toMany {
// ono to one
if parentEntity == nil || (relationshipDescription! as NSRelationshipDescription).destinationEntity != parentEntity! {
// no parent or relationship is "downward" -> object for relationship must be added
var relationshipObject: NSManagedObject? = managedObject!.valueForKey( relationshipName) as? NSManagedObject
var relationshipObjectDictionary: NSMutableDictionary = self.dataStructureFromManagedObject( relationshipObject, parentEntity: managedObject?.entity)
valuesDictionary.setObject( relationshipObjectDictionary, forKey: relationshipName)
} else {
// relationship is "upward" -> nothing to do
}
} else {
// one to many -> all objects must be added
var relationshipObjects: NSSet = managedObject!.mutableSetValueForKey( relationshipName)
var relationshipArray:NSMutableArray = []
for relationshipObjectRaw in relationshipObjects {
var relationshipObject:NSManagedObject? = relationshipObjectRaw as? NSManagedObject
if relationshipObject != nil && !relationshipObject!.entity.isKindOfEntity( managedObject!.entity) {
relationshipArray.addObject(self.dataStructureFromManagedObject( relationshipObject, parentEntity: managedObject?.entity))
}
}
valuesDictionary.setObject( relationshipArray, forKey: relationshipName)
}
}
return valuesDictionary
} else {
return NSMutableDictionary()
}
}
func managedObjectFromStructure( structureDictionary: NSDictionary, moc: NSManagedObjectContext, parentObject: NSManagedObject? = nil) -> NSManagedObject {
if structureDictionary.count > 0 {
var objectName:NSString = structureDictionary.objectForKey( "ManagedObjectName") as NSString
var managedObject:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName( objectName, inManagedObjectContext: moc) as NSManagedObject
var relationshipsByName: NSDictionary = managedObject.entity.relationshipsByName
var realObjectStructure:NSMutableDictionary = structureDictionary.mutableCopy() as NSMutableDictionary
realObjectStructure.removeObjectForKey( "ManagedObjectName")
for key in realObjectStructure.allKeys {
// search for "ManagedObjectName" relationship entrys and delete them before filling the managedObject from this structure
for relationshipName in relationshipsByName.allKeys {
if relationshipName as NSString == key as NSString {
realObjectStructure.removeObjectForKey( key)
}
}
}
managedObject.setValuesForKeysWithDictionary( realObjectStructure)
// the main object with attributes is created. Now care about the relationships
for relationshipName in managedObject.entity.relationshipsByName.keys {
var description:NSRelationshipDescription = relationshipsByName.objectForKey( relationshipName) as NSRelationshipDescription
if !description.toMany {
// to one relationship
if parentObject == nil || description.destinationEntity != parentObject!.entity {
// no parent or relationship is "downward" -> recurse structure to add
var childStructureDictionary:NSDictionary = structureDictionary.objectForKey( relationshipName) as NSDictionary
if childStructureDictionary.count > 0 {
// dictionary not empty -> object must be created and added
var childObject:NSManagedObject? = self.managedObjectFromStructure( childStructureDictionary, moc: moc, parentObject: managedObject)
// validateForUpdate
var error:NSError?
if !managedObject.validateForUpdate( &error) {
println("Error: Object not in valid state for update!!! -> \(error)")
} else {
managedObject.setValue( childObject, forKey: relationshipName as NSString)
}
} else {
// relationship is "upward" -> nothing to do
}
}
} else {
// to many relationship
var relationshipSet:NSMutableSet = managedObject.mutableSetValueForKey( relationshipName as NSString)
var relationshipArray:NSArray = structureDictionary.objectForKey( relationshipName as NSString) as NSArray
for childStructureDictionary in relationshipArray {
if childStructureDictionary.count > 0 {
// dictionary not empty -> object must be created and added
var childObject:NSManagedObject = self.managedObjectFromStructure( childStructureDictionary as NSDictionary, moc: moc, parentObject: managedObject)
// validateForUpdate
var error:NSError?
if !managedObject.validateForUpdate( &error) {
println( "Error: Object not in valid state for update!!! -> \(error)")
} else {
relationshipSet.addObject( childObject)
}
} else {
// no object was behind the relationship -> nothing to do
}
}
// save set
managedObject.setValue( relationshipSet, forKey: relationshipName as NSString)
}
}
// final check validateForUpdate
var error:NSError?
if !managedObject.validateForUpdate( &error) {
println( "Error: Object not in valid state for update although all previous check are passed!!! -> \(error)")
}
return managedObject
} else {
println( "Error: structure for object was empty. this should not happen at this point")
var objectName:NSString = structureDictionary.objectForKey( "ManagedObjectName") as NSString
var managedObject:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName( objectName, inManagedObjectContext: moc) as NSManagedObject
return managedObject
}
}
func dataStructuresFromManagedObjects( managedObjects: NSArray) -> NSArray {
var dataArray:NSMutableArray = []
for managedObject in managedObjects {
dataArray.addObject( self.dataStructureFromManagedObject(managedObject as? NSManagedObject))
}
return dataArray
}
这里的关键是将父实体作为参数传递给递归,因此我们可以决定必须填充数据的关系。因此,这两个功能:dataStructureFromManagedObject
和managedObjectFromStructure
可以将CoreData中的任何实体对象编码和解码为字典,然后再解码为对象。
NSJSONSerialization
developer.apple.com/library/mac/#documentation/Foundation/... stackoverflow.com/questions/6726899/nsjsonserialization-in-ios5