ios Restkit - 使用另一个列表中的列表映射对象

时间:2013-05-10 00:46:22

标签: ios json object mapping restkit

我正在尝试使用Restkit映射对象。我没有映射正常JSON响应的问题。在这种情况下,我有一个问题,因为我想用这个配置映射响应:

{
    "id": 161,
    "text": "usertest",
    "test": {
        "id": "1",
        "beginDate": "17/04/2013",
        "expectedDuration": "45 minutes",
        "finishDate": "17/04/2013",
        "groups": [
            {
                "id": 71,
                "text": "some text",
                "number":"number"
            },
            {
                "id": 81,
                "text": "some anothertext
            }
        ]
        ...
}

如您所见,我有一个主要对象有三个属性(id,text和一系列测试)。每个测试都有一些属性(id,beginDate,...,)。每个测试还有一个组列表。这是我的问题。当我尝试映射时,restkit无法映射“组”列表。

我正在使用此代码进行映射:

//### TEST
RKEntityMapping *testCompleteMapping = [RKEntityMapping mappingForEntityForName:@"UserTest" inManagedObjectStore:managedObjectStore];
[testCompleteMapping addAttributeMappingsFromDictionary:@{
 @"id":             @"usertestID",
 }];

RKEntityMapping *usertest_TestMapping = [RKEntityMapping mappingForEntityForName:@"Test" inManagedObjectStore:managedObjectStore];
[usertest_TestMapping addAttributeMappingsFromDictionary:@{
 @"id":             @"testID",
 @"beginDate":            @"beginDate",
 @"expectedDuration":@"expectedDuration",
 @"finishDate":         @"finishDate",
 }];

RKRelationshipMapping* usertest_Test_Relationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"test" toKeyPath:@"test" withMapping:usertest_TestMapping];

//### GROUPS

RKEntityMapping *groupTestMapping = [RKEntityMapping mappingForEntityForName:@"QuestionGroup" inManagedObjectStore:managedObjectStore];
[groupTestMapping addAttributeMappingsFromDictionary:@{
 @"id":             @"groupID",
 @"number":            @"number",
 @"text":@"text",
 }];

RKRelationshipMapping* group_Test_Relationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"groups" toKeyPath:@"groups" withMapping:usertest_TestMapping];


//### ADD PROPERTY MAPPINGS 

RKResponseDescriptor *CompleteTestResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:testCompleteMapping pathPattern:@"/api/module/demo2/lesson/Phasellus/test/71" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[testCompleteMapping addPropertyMappingsFromArray:[NSArray arrayWithObjects:usertest_Test_Relationship, nil]];
[usertest_TestMapping addPropertyMapping:group_Test_Relationship];

[objectManager addResponseDescriptor:CompleteTestResponseDescriptor];

我也尝试使用“test.groups”而不是“groups”,但它没有用。

1 个答案:

答案 0 :(得分:2)

尝试以下代码。你的主要问题似乎是一个错字(在group_Test_Relationship的定义中),你也缺少一些识别属性(并非严格要求),但通常代码在正确的路径上。

您没有显示testCompleteMapping的定义,因此您需要添加该内容并检查拼写错误。

//### TEST

RKEntityMapping *usertest_TestMapping = [RKEntityMapping mappingForEntityForName:@"Test" inManagedObjectStore:managedObjectStore];
[usertest_TestMapping addAttributeMappingsFromDictionary:@{
 @"id": @"testID",
 @"beginDate": @"beginDate",
 @"expectedDuration": @"expectedDuration",
 @"finishDate": @"finishDate",
 }];
 usertest_TestMapping.identificationAttributes = @[ @"testID" ];

//### GROUPS

RKEntityMapping *groupTestMapping = [RKEntityMapping mappingForEntityForName:@"QuestionGroup" inManagedObjectStore:managedObjectStore];
[groupTestMapping addAttributeMappingsFromDictionary:@{
 @"id": @"groupID",
 @"text": @"text",
 }];
 groupTestMapping.identificationAttributes = @[ @"groupID" ];

[usertest_TestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"groups" toKeyPath:@"groups" withMapping:groupTestMapping]];


// complete your 'testCompleteMapping' here, adding the 'test' relationship property mapping to 'usertest_TestMapping' if required


//### ADD PROPERTY MAPPINGS 

RKResponseDescriptor *completeTestResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:testCompleteMapping pathPattern:@"/api/module/demo2/lesson/Phasellus/test/71" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:completeTestResponseDescriptor];