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

前端 未结 9 1163
悲&欢浪女
悲&欢浪女 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 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;
    }
    

提交回复
热议问题