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

前端 未结 9 1161
悲&欢浪女
悲&欢浪女 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: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]);
    

提交回复
热议问题