0

新しいrestkit 10.0のRKObjectMappingと混同しています。RKClient を使用して Web サービスを呼び出し、JSON 形式で応答を取得しました。ここで、JSON 文字列を User クラスにマップします。どうやってするの ?

{"id":2,"displayName":"Giri Bhushan","roles":null,"username":"FBUser-123456","password":"dddddddddddddddd=\r\n","email":"test@gmail.com","gender":"M","birthDate":null,"friends":null,"bloodGroup":null,"firstName":null,"lastName":null,"phoneNumber":null}

以下のコードで試しました:

RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]];
[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 にマップするにはどうすればよいですか?

4

2 に答える 2

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

于 2012-07-02T16:15:28.457 に答える
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:@""];
于 2012-06-06T12:38:41.557 に答える