Deserializing local NSString of JSON into objects via RestKit (no network download)

前端 未结 9 1123
悲&欢浪女
悲&欢浪女 2020-12-14 20:49

Is it possible to deserialize an NSString of JSON into objects via RestKit? I checked the API list here and could not find something that would serve for this p

相关标签:
9条回答
  • 2020-12-14 20:59

    As of RestKit 0.20.0-pre2

    NSString* JSONString = @"{ \"name\": \"The name\", \"number\": 12345}";
    NSString* MIMEType = @"application/json";
    NSError* error;
    NSData *data = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:MIMEType error:&error];
    if (parsedData == nil && error) {
        // Parser error...
    }
    
    AppUser *appUser = [[AppUser alloc] init];
    
    NSDictionary *mappingsDictionary = @{ @"someKeyPath": someMapping };
    RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
    mapper.targetObject = appUser;
    NSError *mappingError = nil;
    BOOL isMapped = [mapper execute:&mappingError];
    if (isMapped && !mappingError) {
        // Yay! Mapping finished successfully
        NSLog(@"mapper: %@", [mapper representation]);
        NSLog(@"firstname is %@", appUser.firstName);
    }
    
    0 讨论(0)
  • 2020-12-14 21:01

    This works for Restkit 0.21.0:

    NSString* jsonFilePath = [[NSBundle mainBundle] pathForResource:@"fileName"
                                                     ofType:@"json"];
    
    NSString* JSONString = [NSString stringWithContentsOfFile:jsonFilePath
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];
    
    
    NSError* error;
    NSData *data = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    id parsedData = [RKMIMETypeSerialization objectFromData:data MIMEType:RKMIMETypeJSON error:&error];
    if (parsedData == nil && error) {
        // Parser error...
    }
    
    //_objectManager is RKObjectManager instance
    NSMutableDictionary *mappingsDictionary = [[NSMutableDictionary alloc] init];
    for (RKResponseDescriptor *descriptor in _objectManager.responseDescriptors) {
        [mappingsDictionary setObject:descriptor.mapping forKey:descriptor.keyPath];
    }
    
    RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:mappingsDictionary];
    NSError *mappingError = nil;
    BOOL isMapped = [mapper execute:&mappingError];
    if (isMapped && !mappingError) {
        NSLog(@"result %@",[mapper mappingResult]);
    }
    
    0 讨论(0)
  • 2020-12-14 21:03

    Pretty "simple":

    NSString *stringJSON;
    ...
    
    RKJSONParserJSONKit *parser;
    NSError *error= nil;
    parser= [[[RKJSONParserJSONKit alloc] init] autorelease]; 
    MyManagedObject *target;
    target= [MyManagedObject object];
    
    NSDictionary *objectAsDictionary;
    RKObjectMapper* mapper;
    objectAsDictionary= [parser objectFromString:stringJSON error:&error];
    mapper = [RKObjectMapper mapperWithObject:objectAsDictionary 
                              mappingProvider:[RKObjectManager sharedManager].mappingProvider];
    mapper.targetObject = target;
    RKObjectMappingResult* result = [mapper performMapping];
    NSLog(@"%@", [result asObject]);
    
    0 讨论(0)
  • 2020-12-14 21:03

    Is this not what you're looking for? http://restkit.org/api/0.10.0/Classes/RKJSONParserJSONKit.html

    0 讨论(0)
  • 2020-12-14 21:07

    A more iOS 5+ oriented answer:

    NSString* JSONString = jsonString;
    NSString* MIMEType = @"application/json";
    NSError* error = nil;
    id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
    id parsedData = [parser objectFromString:JSONString error:&error];
    if (parsedData == nil && error) {
        NSLog(@"ERROR: JSON parsing error");
    }
    
    RKObjectMappingProvider* mappingProvider = [RKObjectManager sharedManager].mappingProvider;
    RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:mappingProvider];
    RKObjectMappingResult* result = [mapper performMapping];
    if (result) {
    
        NSArray *resultArray = result.asCollection;
    
        MyObject *object = [resultArray lastObject];
        NSLog(@"My Object: %@", object);
    }
    
    0 讨论(0)
  • 2020-12-14 21:07

    For Restkit 0.22, You can use this code. This returns an RKMappingResult wherein you can enumerate the objects after mapping using the property .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;
    }
    
    0 讨论(0)
提交回复
热议问题