使用JSON字符串将使用RestKit的对象手动加载到Core Data中

时间:2012-10-09 06:00:12

标签: iphone ios core-data restkit

使用restkit的开发分支(0.20),有没有办法将JSON字符串映射到核心数据对象存储中?

我知道在0.10中,这个page底部描述的方法有效,但是如何在restkit 0.20中执行此操作?任何指导赞赏!谢谢!

id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
id parsedData = [parser objectFromString:JSONString error:&error];

RKObjectMappingProvider* mappingProvider = [RKObjectManager sharedManager].mappingProvider;
RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider];
RKObjectMappingResult* result = [mapper performMapping];

3 个答案:

答案 0 :(得分:6)

我昨天安装了RESTKit v0.20.0pre4,对一个特定情况也有同样的需求。

以下是我要映射的JSON字符串的示例:

{"info":"the sun is shining","detail":"in Bordeaux ~ 29 °C"}

这是我执行映射的方式。

- (InfoRESTMapped *)mapInfoFromJSONString:(NSString *) JSONString{

NSString* MIMEType = @"application/json";
NSError* error;

NSData *data = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
if (parsedData == nil && error) {
    //deal with error
}

RKObjectMapping *infoMapping = [RKObjectMapping requestMapping];
[infoMapping addAttributeMappingsFromDictionary:@{
 @"info": @"myInfo",
 @"detail": @"myDetail",
 }];


InfoRESTMapped *infoMapped = [[InfoRESTMapped alloc] init];
RKMappingOperation* mapper = [[RKMappingOperation alloc] initWithSourceObject:parsedData destinationObject:infoMapped mapping:infoMapping];
[mapper performMapping:&error];

return infoMapped;
}

这些帖子让我得到了我建议的解决方案:

我希望它有所帮助。我猜其他人会在最终版本发布时给出更好的解决方案(也有更好的文档......)。

答案 1 :(得分:1)

对于Restkit 0.22,您可以使用此代码。这将返回一个RKMappingResult,您可以在使用属性.array进行映射后枚举对象。

- (RKMappingResult *)mapJSONStringWithString:(NSString *)jsonString
{
     RKMappingResult *result = nil;

     NSError* error;
     NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
     id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:RKMIMETypeJSON error:&error];
     if (parsedData == nil && error) {
        NSLog(@"json mapping error");
     }

     NSDictionary *mappingsDictionary = @{@"":[CustomMappingClass getMappingForUsers]};

     ObjectClass *obj = [ObjectClass new];
     RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
     NSError *mappingError = nil;
     mapper.targetObject = obj;
     BOOL isMapped = [mapper execute:&mappingError];
     if (isMapped && !mappingError) {
         result = [mapper mappingResult];
     }

    return result;
}

答案 2 :(得分:1)

Read Me&#34;生成种子数据库&#34;

中记录了使用RestKit将JSON导入核心数据存储的最直接方法。

这是我在Swift中写的:

if let persistentStore = RKManagedObjectStore.default().persistentStoreCoordinator.persistentStores.first {
    if let objectImporter = RKManagedObjectImporter.init(persistentStore: persistentStore) {
        let path = Bundle.main.path(forResource: jsonFile, ofType: "json")
        let mapping = SLTour.responseMapping()
        var error:NSError?
        objectImporter.importObjectsFromItem(atPath: path, with: mapping, keyPath: "data", error: &error)
        try objectImporter.finishImporting()
        objectImporter.logSeedingInfo()
    }
}

希望这可以节省一些人在未来几个小时内试图解决这个问题。