RestKit: dynamic nested attributes with an array

I'm struggling to find a way to map some JSON in RestKit. This is an example of what I'm looking for:

       "results":{
           "Test1":[
              {
                 "id":1,
                 "name":"Test 1 here.",
                 "language":"English",
                 "type: "Test1"
              }
           ],
           "Test2":[
              {
                 "id":3,
                 "name":"Another test 2",
                 "language":"English",
                 "type":"Test2"
              },
              {
                 "id":8,
                 "name":"More test 2",
                 "language":"English",
                 "type":"Test2"
              },
              {
                 "id":49,
                 "name":"foo",
                 "language":"English",
                 "type":"Test2"
              }
           ]
        }

Ideally, JSON will not include an extra redundant "like" layer, but this is life.

I would like RestKit to return 4 objects under "results" like:

@interface Test : NSObject

@property (nonatomic, copy) NSNumber *testId;
@property (nonatomic, copy) NSString *testName;
@property (nonatomic, copy) NSString *testLanguage;
@property (nonatomic, copy) NSString *testType;

I tried different combinations of mappings, for example:

RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[Test class]];
testMapping.forceCollectionMapping = YES;
[testMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"testType"];
[testMapping addAttributeMappingsFromDictionary:@{
                                                      @"(testType).id": @"testId",
                                                      @"(testType).name": @"testName",
                                                      @"(testType).language": @"testLanguage",
                                                      }];

but it still fails because its not the only object under the "JSON" type key - it's an array of Test objects.

Is there any way to present this mapping in RestKit? Or, if not, be able to redefine some callback functions so that I can make it work? Unfortunately, I cannot change the JSON data coming from the server

+3
1

, @"results" . , NSDictionary . , ().

, , testMapping, addAttributeMappingFromKeyOfRepresentationToAttribute, ( type).

setObjectMappingForRepresentationBlock:, representation, NSDictionary JSON. , , , .


RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[Test class]];
[testMapping addAttributeMappingsFromDictionary:@{ @"id": @"testId", @"name": @"testName" }];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
    RKObjectMapping *testListMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
    for (NSString *key in representation) {
        [testListMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeypath:key toKeyPath:key withMapping:testMapping];
    }

    return testListMapping;
}];
+2

Source: https://habr.com/ru/post/1692126/


All Articles