Võ Huy Hưng
Võ Huy Hưng

Reputation: 93

How to mapping object by RestKit 0.20.x with complicate structure JSON

I'm using Restkit 0.20.x to mapping object from json like

{
    "d":{
        "results":[
            {
                "Web":[
                    "key1":"value1",
                    "key2":"value2"
                ],
                "Image":[
                    "key1":"value1",
                    "key2":"value2"
                ],
            },
        ],
    },
}

My main purpose to manage "Web" and "Image" key. I'm trying to mapping object but stuck at "results" key (value of key "results" is a array which has only one element as dictionary). How to using RestKit to map object in my case ?

My fail implement:

WFSD.h

@interface WFSD : NSObject  
@property (nonatomic, strong) WFSResults *results;
@end

WFSResults.h

@interface WFSResults : NSObject
@property (nonatomic, strong) WFSResult    *result;
@end

WFSResult.h

@interface WFSResult : NSObject
@property (nonatomic, strong) WFSWeb    *web;
@property (nonatomic, strong) WFSImage  *image;
@end

MyController.m

RKObjectMapping* dMapping = [RKObjectMapping mappingForClass:[WFSD class]];
RKObjectMapping* resultsMapping = [RKObjectMapping mappingForClass:[WFSResults class]];

RKRelationshipMapping* rsMapping1 = [RKRelationshipMapping relationshipMappingFromKeyPath:@"results" toKeyPath:@"results" withMapping:resultsMapping];
[dMapping addPropertyMapping:rsMapping1];

RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[WFSResult class]];
[resultsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"result" withMapping:resultMapping]];

RKRelationshipMapping* rsMapping2 = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Image" toKeyPath:@"Image" withMapping:imageMapping];
[resultMapping addPropertyMapping:rsMapping2];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor     responseDescriptorWithMapping:dMapping
                                                                                       pathPattern:nil
                                                                                           keyPath:@"d"
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

Upvotes: 0

Views: 416

Answers (1)

Wain
Wain

Reputation: 119041

Look at removing WFSResults from your model classes. Really it's just a list of WFSResult objects, so you should look at modelling it as:

@interface WFSD : NSObject  
@property (nonatomic, strong) NSArray *results;
@end

You also need to look at WFSResult, because Web and Image in the JSON are also arrays. So I'd expect to see:

@interface WFSResult : NSObject
@property (nonatomic, strong) NSArray  *web;
@property (nonatomic, strong) NSArray  *image;
@end

In this way RestKit can create the objects during mapping and then it has an array to store the list of objects.

Upvotes: 2

Related Questions