2012-04-19 90 views
0

我對新的restkit 10.0中的RKObjectMapping感到困惑。 我已經用RKClient調用了我的webservice,並以JSON的形式得到響應。 現在我想用我的User類來映射JSON字符串。 如何做到這一點?restkit 10.0中的RKObjectMapping映射問題

塊引用 { 「ID」:2 「顯示名」: 「吉瑞普山」, 「角色」:NULL, 「用戶名」: 「FBUser-123456」, 「密碼」:「dddddddddddddddd = \ r \ N」, 「電子郵件」: 「[email protected]」, 「性別」: 「M」, 「生日」:空, 「朋友」:空, 「bloodGroup」:空, 「名字」:空, 「姓氏」 :NULL, 「phoneNumber的」 日期null}

我試圖與下面的代碼:

RKObjectMapping * userMapping = [RKObjectMapping mappingForClass:[用戶類]]; [userMapping mapKeyPath:@「id」toAttribute:@「userId」];

[userMapping mapKeyPath:@"displayName" toAttribute:@"displayName"]; 
[userMapping mapKeyPath:@"userName" toAttribute:@"userName"]; 
[userMapping mapKeyPath:@"password" toAttribute:@"password"]; 
[userMapping mapKeyPath:@"email" toAttribute:@"email"]; 
[userMapping mapKeyPath:@"gender" toAttribute:@"gender"]; 
[userMapping mapKeyPath:@"birthDate" toAttribute:@"birthDate"]; 
[userMapping mapKeyPath:@"bloodGroup" toAttribute:@"bloodGroup"]; 
[userMapping mapKeyPath:@"firstName" toAttribute:@"firstName"]; 
[userMapping mapKeyPath:@"lastName" toAttribute:@"lastName"]; 

[userMapping mapKeyPath:@"phoneNumber" toAttribute:@"phoneNumber"]; 

[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"]; 

但是,我怎麼映射JSON字符串RKObjectMapping?

回答

0

如果您在您的JSON的根級別用戶,下面應該工作:

RKObjectManager *manager = [RKObjectManager sharedManager]; 
RKManagedObjectMapping *userMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:manager.objectStore]; 
[userMapping mapKeyPath:@"id" toAttribute:@"userId"]; 
[userMapping mapAttributes:@"displayName",@"userName",@"password", nil]; // Add other attributes 
[manager.mappingProvider setMapping:userMapping forKeyPath:@""]; 
0

你缺少這一行:
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"" delegate:self];

然後執行類似的委託方法:
(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects{ NSLog(@"Objects Retrieved: %d\n%@", objects.count, objects);
YourObject* obj = [objects objectAtIndex:0];

NSString* info = [NSString stringWithFormat: 
        @"Value 1 is %@\n" 
        @"Value 2 is %@\n" 
        @"Value 3 is: %@", 
        [obj value1], 
        [obj value2], 
        [obj value3]; 

}`

希望這對你有用。 XD