0

I'm sending a request about an user. There are several possibilities. I'm thinking about sending a JSON like that:

"type1": {
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}
"type2": {
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}
"type3": {
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}

Then I was thinking about a multiple RKResponseDescriptor but I don't know how to interpret the RKMappingResult. Do I have a way to refer an object to the keyPath ? Here I'm using 3 times the same RKEntityMapping but with different keyPaths.

Finally I don't even know if it's possible.

Edit:

Maybe this kind of JSON is better and easier to understand/map.

"type" : "type1", 
"user": {
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}

Thank you for your help.

2
  • That doesn't look like very nice JSON. But it really isn't clear what you're asking here... Are you asking about a request or a response? Are you misunderstanding path patterns or key paths? I have no idea currently.
    – Wain
    Jun 23, 2014 at 14:17
  • Honestly I don't even know how to do it (for both the request or the response). Maybe a simple parameter inside the JSON could be better but can I add as descriptor a for a known entity and just a new parameter ? I've added another example of possible JSON.
    – brcebn
    Jun 23, 2014 at 18:12

3 Answers 3

0

There isn't enough information to suggest about JSON structure. The arbitrary keys isn't very nice, but it is quite common. Generally a 'container' object in the JSON is nicer.

That said, RestKit can generally handle both. Check out addAttributeMappingFromKeyOfRepresentationToAttribute: and addAttributeMappingToKeyOfRepresentationFromAttribute: on RKObjectMapping for examples of how to handle that kind of JSON.

0
0

If your question is related to how to map the response JSON using Restkit mapping then this is better way to design your JSON.

{"userProfile":[
{
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
},
{
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
},
 {
    "id": 34,
    "email": "[email protected]",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}
]}

Modal classes:

userProfile.h

@property(nonatomic,strong) NSMutableArray *userInfo;

userInfo.h

@property(assign) int id;
@property(nonatomic,strong) NSString *email;

.......

Mapping can be done with AttributeMapping and RelationshipMapping

0

I finally mapped the type like this:

RKObjectMapping *typeMapping = [RKObjectMapping requestMapping];
[typeMapping addAttributeMappingsFromDictionary:@{@"type":@"type"}];

With this descriptor:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:typeMapping
                                                                                         method:RKRequestMethodAny
                                                                                    pathPattern:nil
                                                                                        keyPath:nil
                                                                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

Basically to catch this dictionary, basically I did this:

NSString *type = [((NSDictionary *)[[mappingResult array] objectAtIndex:0]) objectForKey:@"type"];

This solve my problem because just after I'm selecting different actions according to the value of type.

I let @Wain tell us if this method is acceptable or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.