无法将字段映射到 JSON 和 JSON 到 class RESTKit 中的字段

Unable to map the fields to JSON and JSON to fields in class RESTKit

我有以下代码,我正在调用 RESTful API 并注册一个用户,但它给我错误,如下所述

 // Construct a request mapping for our class
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{ @"strFirstName": @"FirstName", @"strLastName": @"LastName",@"strEmail":@"Email",@"strPhoneNumber":@"PhoneNumber",@"strToken":@"Token",@"strPassword":@"Password" }];

// We wish to generate parameters of the format:
// @{"Client":{"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Token":"String"}}
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                               objectClass:[Client class]
                                                                               rootKeyPath:@"Client" method:RKRequestMethodPOST];

// Construct an object mapping for the response
// We are expecting JSON in the format:
//{"Client":{"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Token":"String"},"ErrorMessage":"String","FriendlyMessage":"String"}
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Client class]];

[responseMapping addAttributeMappingsFromDictionary:@{ @"FirstName": @"strFirstName", @"LastName": @"strLastName",@"Email":@"strEmail",@"PhoneNumber":@"strPhoneNumber",@"Token":@"strToken",@"Password":@"strPassword" }];

// Construct a response descriptor that matches any URL (the pathPattern is nil), when the response payload
// contains content nested under the `@"page"` key path, if the response status code is 200 (OK)
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                   method:RKRequestMethodPOST
                                                                                   pathPattern:nil
                                                                                       keyPath:@"Client"
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
   // Register our descriptors with a manager
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"BASE_URL"]];

[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];

// Work with the object
Client *client = [Client new];
NewUser *user = [NewUser sharedNewUser];
client.strFirsName = user.strFname;
client.strLastName = user.strLname;
client.strPassword = user.strPwd;
client.strPhoneNumber = user.strNum;
client.strEmail = user.strEmail;

// POST the parameterized representation of the `page` object to `/posts` and map the response
[manager postObject:client path:@"/services/servicestack/RegisterClient.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSLog(@"We object mapped the response with the following result: %@", result);
} failure:nil];

也是客户端class

 #import <Foundation/Foundation.h>
    #import "RESTObject.h"

   @interface Client : RESTObject
   {
    NSString *strFirstName;
    NSString *strLastName;
    NSString *strEmail;
    NSString *strPassword;
    NSString *strPhoneNumber;
    NSString *strToken;

   }

   @property (nonatomic,retain) NSString *strFirsName;
   @property (nonatomic,retain) NSString *strLastName;
   @property (nonatomic,retain) NSString *strEmail;
   @property (nonatomic,retain) NSString *strPassword;
   @property (nonatomic,retain) NSString *strPhoneNumber;
   @property (nonatomic,retain) NSString *strToken;

   @end

RESTObject class

@interface RESTObject : NSObject
{

}

@property (nonatomic,retain) NSString *strErrorMessage;
@property (nonatomic,retain) NSString *strFriendlyMessage;

@end

我得到的错误如下

restkit.network:RKObjectRequestOperation.m:243 POST 'http://101.178.90.12/chauffr_services/servicestack/RegisterClient.json' (200 OK / 0 objects) [request=0.7125s mapping=0.0000s total=0.7358s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x78e88e40 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: Client
The representation inputted to the mapper was found to contain nested object representations at the following key paths: ErrorMessage
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}

预期输入 JSON 是

POST /json/reply/RegisterClientRequest HTTP/1.1 Host: 101.178.90.12 Content-Type: application/json Content-Length: length

{"Client":{"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Token":"String"}}

预期输出 JSON 是

HTTP/1.1 200 OK Content-Type: application/json Content-Length: length

{"Client":{"FirstName":"String","LastName":"String","Email":"String","Password":"String","PhoneNumber":"String","Token":"String"},"ErrorMessage":"String","FriendlyMessage":"String"}

我可能犯了一些非常愚蠢的错误,但我无法弄清楚。

您看到的错误是:

The mapping operation was unable to find any nested object representations at the key paths searched: Client
The representation inputted to the mapper was found to contain nested object representations at the following key paths: ErrorMessage

因此,虽然您期望响应包含 'Client' 数据,但它没有,它包含 'Error',因此 RestKit 无法找到您要求它处理的任何内容。

显然你需要找出服务器 returns 错误的原因,但你也可以添加错误映射,或者,如果总是返回成功状态代码,则更有可能使用动态映射来选择做什么,然后使用映射的键路径进行适当处理。