iPhone RestKit 是不是可以将核心数据对象图转换为 JSON?
Posted
技术标签:
【中文标题】iPhone RestKit 是不是可以将核心数据对象图转换为 JSON?【英文标题】:iPhone RestKit is it possible to convert a Core Data object graph into JSON?iPhone RestKit 是否可以将核心数据对象图转换为 JSON? 【发布时间】:2012-04-23 13:21:14 【问题描述】:我正在为我的一个项目使用 RestKit 框架。它包括一个如何使用对象映射将 JSON 对象转换为核心数据图的示例。如果可能相反,我正在徘徊 - 可以将使用 RestKit 的核心数据实体转换回 JSON 表示形式吗?
我找到了 RKObjectSerializer
类,但我似乎无法让它工作 - 我得到的序列化对象是 nil
-(void)doCoreDataToJSONConversion
Article* article = [_articles objectAtIndex:0];
RKManagedObjectMapping* articleMapping = [RKManagedObjectMapping mappingForClass:[Article class]];
NSAssert(articleMapping!=nil,@"article mapping is nil!");
NSLog(@"%@",[article description]);
RKObjectSerializer* serializer =[RKObjectSerializer serializerWithObject:article mapping:articleMapping];
NSError* error = nil;
NSMutableDictionary* serializedObject = [serializer serializedObject:&error];
if(error!=nil)
NSLog(@"!!!!! Error: %@",[error localizedDescription]);
//prints nil
NSLog(@"Serialized Object: %@", [serializedObject description]);
感谢您的意见!
【问题讨论】:
【参考方案1】:无法准确地告诉您有关 RestKit 的信息,但您可以轻松地将所有核心数据图转换为 JSON 并返回,只要您看看 Marcus Zarra 的这个绝对精彩的答案
JSON and Core Data on the iPhone
【讨论】:
【参考方案2】:我最终使用这些方法将核心数据对象转换为 JSON。要反序列化这些对象,我需要定义不同的映射类:RKManagedObjectMapping,它与常规对象映射略有不同。
-(void)setupObjectSerializationMapping
RKObjectManager *objectManager = [RKObjectManager sharedManager ] ;
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Article class]];
[mapping mapAttributes:@"articleID", @"title", @"body", nil];
//********************************
RKObjectMapping *imageMapping = [RKObjectMapping mappingForClass:[EventImage class]];
[imageMapping mapAttributes:@"createDate", @"localFilePath", nil];
[objectManager.mappingProvider addObjectMapping:imageMapping];
[objectManager.mappingProvider setSerializationMapping:[imageMapping inverseMapping] forClass:[EventImage class]];
[objectManager.mappingProvider setMapping:imageMapping forKeyPath:@"eventImages"];
//********************************
RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Event class]];
[eventMapping mapAttributes:@"createDate", @"note", nil];
[eventMapping mapRelationship:@"eventImages" withMapping:imageMapping];
[objectManager.mappingProvider addObjectMapping:eventMapping];
[objectManager.mappingProvider setSerializationMapping:[eventMapping inverseMapping] forClass:[Event class]];
[objectManager.mappingProvider setMapping:eventMapping forKeyPath:@"events"];
//********************************
//setup App user mapping
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[AppUserSubclass class]];
[userMapping mapAttributes:@"userID",@"firstName", @"lastName",@"localDataFilepath", nil];
[userMapping mapRelationship:@"events" withMapping:eventMapping];
[objectManager.mappingProvider addObjectMapping:userMapping];
[objectManager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[AppUserSubclass class]];
[objectManager.mappingProvider setMapping:userMapping forKeyPath:@"appUser"];
-(NSString*)generateLocalJSONData
NSString* folderPath = [self dataFolderPath];
// remember what the file was called. This will not include the documents directory and will be the same on all machines.
NSString* localFileName = [self fileName];
NSString* dataFile = [self fullDocumentsFilePath];
self.localDataFilepath = dataFile;
NSAssert(self.localDataFilepath!=nil,@"failed to save local data path");
//********************************
NSError* error = nil;
//app user subclass is the same as AppUser, but is used to differentiate between file based mapping and core data mapping
RKObjectMapping *serMap = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[AppUserSubclass class]];
NSDictionary *d = [[RKObjectSerializer serializerWithObject:self mapping:serMap] serializedObject:&error];
if(error!=nil)
NSLog(@"!!!!! Error: %@",[error localizedDescription]);
NSString* dataContents = [d JSONString];
BOOL success = [dataContents writeToFile:dataFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(!success)
NSLog(@"Error writing to data file!");
[[AppUser managedObjectContext] save:nil];
return dataFile;
【讨论】:
以上是关于iPhone RestKit 是不是可以将核心数据对象图转换为 JSON?的主要内容,如果未能解决你的问题,请参考以下文章
JSONKit +lidenbrock 与。 RestKit?(使用CoreData)