发布到服务器时嵌套对象的 RestKit 错误
RestKit error for a nested object while POSTing to server
我有一项服务要向其发送以下类型的请求
{"ClientDetails":{"ChauffeurId":0,"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Thumbnail":"AA==","PayKey":"String","DeviceKey":"String"},"Token":"String"}
我正在使用以下代码使用 RestKit 映射发送此请求,如图所示,问题是我收到 'Invalid type in JSON write (Client)' 错误,无法弄清楚错误是什么。
RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[Client class]];
[requestMapping addAttributeMappingsFromDictionary:@{ @"FirstName": @"FirstName", @"LastName": @"LastName",@"Email":@"Email",@"PhoneNumber":@"PhoneNumber",@"Token":@"PayKey",@"Password":@"Password",@"ChauffeurId":@"ChauffeurId",@"Image":@"Thumbnail",@"DeviceToken":@"DeviceKey" }];
RKObjectMapping *tokenMapping = [RKObjectMapping requestMapping];
[tokenMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"token" toKeyPath:@"Token"]];
[tokenMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"client"
toKeyPath:@"ClientDetails"
withMapping:requestMapping]];
RKRequestDescriptor *tokenReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:tokenMapping
objectClass:[RegClientRequest class]
rootKeyPath:nil method:RKRequestMethodPOST];
// Register our descriptors with a manager
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:tokenReqDescriptor];
Client *client = [Client new];
NewUser *user = [NewUser sharedNewUser];
client.FirstName = user.strFname;
client.LastName = user.strLname;
client.Password = user.strPwd;
client.PhoneNumber = user.strNum;
client.Email = user.strEmail;
client.Token = user.strToken;
client.DeviceToken = user.strDeviceToken;
RegClientRequest *regClientRequest = [RegClientRequest new];
regClientRequest.token = [ServiceUrls token];
regClientRequest.client = client;
[manager postObject:regClientRequest path:[ServiceUrls registerUser] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
}
问题是您正在创建一个 class 特定映射,您应该在其中使用 [RKObjectMapping requestMapping
或采用 inverseMapping
.
这意味着目前您正在将客户端映射到客户端并尝试将生成的客户端放入 JSON(而不是将客户端映射到字典)。
我有一项服务要向其发送以下类型的请求
{"ClientDetails":{"ChauffeurId":0,"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Thumbnail":"AA==","PayKey":"String","DeviceKey":"String"},"Token":"String"}
我正在使用以下代码使用 RestKit 映射发送此请求,如图所示,问题是我收到 'Invalid type in JSON write (Client)' 错误,无法弄清楚错误是什么。
RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[Client class]];
[requestMapping addAttributeMappingsFromDictionary:@{ @"FirstName": @"FirstName", @"LastName": @"LastName",@"Email":@"Email",@"PhoneNumber":@"PhoneNumber",@"Token":@"PayKey",@"Password":@"Password",@"ChauffeurId":@"ChauffeurId",@"Image":@"Thumbnail",@"DeviceToken":@"DeviceKey" }];
RKObjectMapping *tokenMapping = [RKObjectMapping requestMapping];
[tokenMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"token" toKeyPath:@"Token"]];
[tokenMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"client"
toKeyPath:@"ClientDetails"
withMapping:requestMapping]];
RKRequestDescriptor *tokenReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:tokenMapping
objectClass:[RegClientRequest class]
rootKeyPath:nil method:RKRequestMethodPOST];
// Register our descriptors with a manager
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:tokenReqDescriptor];
Client *client = [Client new];
NewUser *user = [NewUser sharedNewUser];
client.FirstName = user.strFname;
client.LastName = user.strLname;
client.Password = user.strPwd;
client.PhoneNumber = user.strNum;
client.Email = user.strEmail;
client.Token = user.strToken;
client.DeviceToken = user.strDeviceToken;
RegClientRequest *regClientRequest = [RegClientRequest new];
regClientRequest.token = [ServiceUrls token];
regClientRequest.client = client;
[manager postObject:regClientRequest path:[ServiceUrls registerUser] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
}
问题是您正在创建一个 class 特定映射,您应该在其中使用 [RKObjectMapping requestMapping
或采用 inverseMapping
.
这意味着目前您正在将客户端映射到客户端并尝试将生成的客户端放入 JSON(而不是将客户端映射到字典)。