Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用RestKit v0.21映射原始json响应_Json_Restkit_Restkit 0.20 - Fatal编程技术网

使用RestKit v0.21映射原始json响应

使用RestKit v0.21映射原始json响应,json,restkit,restkit-0.20,Json,Restkit,Restkit 0.20,我有一个API,它要求我发布一个复杂的JSON对象。API保存并响应基本ID(或错误对象)。我无法映射原始ID。有什么想法吗?为了简单起见,下面的示例没有进行POST对象映射,但是我的一些API调用也需要这样做 我看到了使用RestKit构建请求并将其传递给AFNetworking的建议,但这不会解析可能的错误返回对象 RKObjectMapping* map = [RKObjectMapping mappingForClass:[MyPrimitiveResponse class]]; [ma

我有一个API,它要求我发布一个复杂的JSON对象。API保存并响应基本ID(或错误对象)。我无法映射原始ID。有什么想法吗?为了简单起见,下面的示例没有进行POST对象映射,但是我的一些API调用也需要这样做

我看到了使用RestKit构建请求并将其传递给AFNetworking的建议,但这不会解析可能的错误返回对象

RKObjectMapping* map = [RKObjectMapping mappingForClass:[MyPrimitiveResponse class]];
[map addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"success"]];

RKResponseDescriptor *errDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MyErrorResponse objectMap] method:RKRequestMethodGET  pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError)];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:map method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSURL *URL = [NSURL URLWithString:apiUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ errDescriptor, responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

[objectRequestOperation start];
我在调试器中遇到以下错误:

2013-10-29 10:23:15.196 TestParser[6988:70b]E app:MyHomeViewController.m:54操作失败,错误为:error Domain=org.restkit.restkit.ErrorDomain Code=-1017“加载了一个内容类型为'application/json'的不可处理响应(200)”,UserInfo=0x8daca20{nserrorfailingurkey=…,NSUnderlyingError=0x8db4cd0“操作无法完成。(Cocoa错误3840。)”,NSLocalizedDescription=加载了内容类型为“application/json”的不可处理响应(200)}

更新:
这是我处理这种情况的最后一段代码。它可能对其他人有用

// Manually Map Request to JSON & send to server
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:payload requestDescriptor:[payload.class requestDescriptor] error:&error];
NSMutableURLRequest* request = [self.apiManager requestWithObject:nil method:RKRequestMethodPOST path:url parameters:parameters];

RKHTTPRequestOperation *requestOperation = [[RKHTTPRequestOperation alloc] initWithRequest:request];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    unichar firstChar = [operation.responseString characterAtIndex:0];
    NSData *newData;
    if (firstChar != '{' && firstChar != '[') {
        // Force into JSON array so it can be parsed normally
        newData = [[[@"[" stringByAppendingString:operation.responseString] stringByAppendingString:@"]"] dataUsingEncoding:NSUTF8StringEncoding]; 
    } else {
        newData = operation.responseData;
    }

    // Parse JSON response into native object, whether primitive NSNumber (integer or boolean) response or a full fledged JSON error object.
    RKResponseDescriptor *errDescriptor = [MyErrorResponse responseDescriptor];
    RKObjectResponseMapperOperation* mapper = [[RKObjectResponseMapperOperation alloc] initWithRequest:request response:operation.response data:newData responseDescriptors:@[errDescriptor, [payload.class responseDescriptor]]];
    [mapper setDidFinishMappingBlock:^(RKMappingResult *mappingResult, NSError *error) {
        if (mappingResult) { //Success
            RKLogInfo(@"Load response: %@", mappingResult.firstObject);
        } else {
            RKLogError(@"Operation failed with error: %@", error);
        }
    }];
    [mapper start];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

[requestOperation start];
我看到了利用RestKit构建请求并将其传递给AFNetworking的建议

是的,那样做

但这不会解析可能的错误返回对象

是,但您可以使用RestKit中的映射操作来处理该错误。或者,如果错误响应相对简单,则只需使用
NSJSONSerialization
(或类似)来转换响应



“基元ID”应该可以映射为
nil keypath
映射。但是,如果没有将错误响应映射回POST请求中使用的源对象,则在映射错误响应时可能仍然存在问题。

您可以解释响应的最后一部分吗?源对象应该如何构造以包含响应对象?Sho我是否要添加一个错误元素并将该键映射到某个地方而不是nil?我会选择第一个选项。第二个选项可能会有问题。