2
我試圖用Restkit映射對象。我沒有問題映射正常的JSON響應。在這種情況下,我有一個問題,因爲我要地圖這個配置的響應:ios Restkit - 用另一個列表中的列表映射對象
{
"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,文本和測試列表)主要對象。每個測試都有一些屬性(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」,但沒有奏效。
我忘了顯示testCompleteMapping的定義並顯示正確的JSON代碼(有一個數字字段)。現在,一切都是正確的。我會很快嘗試你的代碼。 – 2013-05-10 09:43:27
你說得對。我做了一個錯誤,創建realtionship。[usertest_TestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@「groups」toKeyPath:@「groups」withMapping:groupTestMapping]];這條線解決了我的問題。 – 2013-05-10 14:32:11