How to change JSON before displaying RESTKIT

I am using RESTKIT to display the JSON returned from the server.

The JSON result obtained on the server is as follows

{"term":"Zh","results":{"users":[{"id":{"$oid":"4ebe59970a614e0019000055"},"term":"some text","score":1}]}

How to convert the above JSON result to the following:

{"results":{"users":[{"uid":"4ebe59970a614e0019000055","text":"some text"}]}

Also, where can I do this so that the RESTKIT mapping uses converted JSON instead of the original?

Below is the bootloader class that I use to manage JSON and mappings

-(void)getObjects
{
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
    NSLog(@"Loaded PAYLOAD successfully for %@, with response %@", self.resourcePath , [response bodyAsString]  );
}


- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects 
{
}


+ (void)setManagerAndMappings
{
    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:SERVER_URL];
    RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];

    //User Object Mapping
    RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping mapKeyPath:@"_id" toAttribute:@"uid"];

    [userMapping mapAttributes:@"avatar_url",@"created_at",@"email",@"name",@"nickname",@"follower_count",@"following_count",@"answer_count",@"new_notification_count",@"new_questions_count",@"is_following",@"facebook_configured",@"twitter_configured",@"description",@"approved",@"type", nil];
    [provider setMapping:userMapping forKeyPath:@"user"];

}
+5
source share
6 answers

For me, the only solution is to change the returned object at the server level. If you cannot, just match what the server returns.

+1
source

@Shocks answer is attractive, unfortunately, it is valid for versions up to 0.20.

0.20, RKSerialization:

@interface ORRKJsonSerialization : NSObject <RKSerialization>
@end

@implementation ORRKJsonSerialization

+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
    id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
    // change your data before mapping
    return result;
}

+ (NSData *)dataFromObject:(id)object error:(NSError **)error
{
    return [NSJSONSerialization dataWithJSONObject:object options:0 error:error];
}

@end

:

[RKMIMETypeSerialization registerClass:[ORRKJsonSerialization class] forMIMEType:@"application/json"];

+4

willMapData: select RKObkectLoaderDelegate, . mappableData , , .

+3

RKObjectRequestOperation.setWillMapDeserializedResponseBlock:.

:

    let request = RKObjectManager.sharedManager().requestWith...
    let operation = RKObjectManager.sharedManager().managedObjectRequestOperationWithRequest(request, managedObjectContext: context, success: { operation, result in
        // success
    }, failure: { operation, error in
        // failure
    })
    operation.setWillMapDeserializedResponseBlock { deserializedResponse -> AnyObject! in
        // Here to transform response
        return transformResponse(deserializedResponse)
    }
    RKObjectManager.sharedManager().enqueueObjectRequestOperation(operation)
+3

. , JSON .

[[RKParserRegistry sharedRegistry] setParserClass:[MyJSONParser class]
                                          forMIMEType:@"application/json"];

:

#import <RestKit/RestKit.h>
#import <RestKit/RKJSONParserJSONKit.h>
#import <RestKit/JSONKit.h>
#import <RestKit/RKLog.h>

@interface EFJSONParser : RKJSONParserJSONKit
- (NSDictionary *)objectFromString:(NSString *)string error:(NSError **)error;
@end
+1

JSOn. - , @beber

0

All Articles