使用RestKit 0.20.3映射json [英] Mapping json with RestKit 0.20.3
问题描述
我正在尝试通过RestKit 0.20.3映射对象,但是我知道这些日志已经有好几天了:
I'm trying to map objects through RestKit 0.20.3, but I'm having those logs for days know:
2013-08-12 18:32:08.158 MyAppIphone[848:5703] E restkit.network:RKResponseMapperOperation.m:304 Failed to parse response data: Loaded an unprocessable response (200) with content type 'application/json'
2013-08-12 18:32:08.174 MyAppIphone[848:5703] E restkit.network:RKObjectRequestOperation.m:238 POST 'myUrl' (200 OK / 0 objects)
[request=0.1305s mapping=0.0000s total=5.6390s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"
UserInfo=0x1ed5a500 {NSErrorFailingURLKey=myUrl, NSUnderlyingError=0x1ed5b240 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/json'}
response.body={"my json content"}
这是MyData类:
#import <Foundation/Foundation.h>
@interface MyData : NSObject
@property (nonatomic, retain) NSString *criterias;
@end
这是我设置映射器的方式:
Here is how I set up my mapper:
- (RKResponseDescriptor*) getDataMapping
{
// Mapping
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[MyData class]];
[mapping addAttributeMappingsFromDictionary:@{
@"criteriasHeader": @"criteriasHeader"
}];
// Status code
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
// Descriptior
return [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodPOST pathPattern:nil keyPath:@"regions" statusCodes:statusCodes];
}
这是我的请求功能:
- (void) runRequestWithType:(RequestType)type baseUrl:(NSString *)baseUrlString path:(NSString *)path parameters:(NSDictionary *)parameters mapping:(RKResponseDescriptor *) descriptor
{
// Print heeader and body from the request and the response
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("Restkit/ObjectMapping", RKLogLevelDebug);
// Set up the base url
NSURL *baseUrl = [NSURL URLWithString:baseUrlString];
//Run request in block
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseUrl];
[manager addResponseDescriptorsFromArray:@[descriptor]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[MyData class] pathPattern:path method:RKRequestMethodPOST]];
//[manager getObjectsAtPath:path parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[manager postObject:nil path:path parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if ([self.delegate respondsToSelector:@selector(requestDidSucceedWithType:andResponse:)]) {
[self.delegate requestDidSucceedWithType:type andResponse:[mappingResult firstObject]];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if ([self.delegate respondsToSelector:@selector(requestDidFailWithError:andError:)]) {
[self.delegate requestDidFailWithType:type andError:error];
}
}];
}
PS:我尝试使用较短的JSON,效果很好.
PS: I tried with a shorter JSON, it works well.
我做错了吗?
谢谢你.
推荐答案
希望您能够将JSON的服务器编码更改为UTF-8.
Hopefully you are able to change the server encoding of the JSON to UTF-8.
如果没有,则可以通过替换Restkit中的默认JSON mime类型处理程序来解决此问题.使用Restkit类RKNSJSONSerialization
作为参考.
If not, you can resolve this issue by replacing the default JSON mime type handler in Restkit. Use the Restkit class RKNSJSONSerialization
as reference.
在自定义JSON mime类型处理程序中,执行与RKNSJSONSerialization
类相同的操作,将数据从传入的编码(在ISO-8859-1下面的示例中)转换为UTF-8.
In your custom JSON mime type handler, perform data conversion from the incoming encoding (in the example below ISO-8859-1) to UTF-8, before doing the same as the RKNSJSONSerialization
class.
@implementation MyCustomJSONSerializer
+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
NSString* latin = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSISOLatin1StringEncoding];
NSData* utf8 = [latin dataUsingEncoding:NSUTF8StringEncoding];
return [NSJSONSerialization JSONObjectWithData:utf8 options:0 error:error];
}
@end
如果必须以非UTF-8编码将数据回传到服务器,则可以对dataFromObject
方法执行类似的操作.
You can do similar for the dataFromObject
method if you must POST data back to the server in non-UTF-8 encoding.
您现在可以添加此自定义处理程序,然后将其与默认的(UTF-8)使用:
You can now add this custom handler after you init Restkit, and it will be used vs the default (UTF-8) one:
[RKMIMETypeSerialization registerClass:[MyCustomJSONSerializer class] forMIMEType:RKMIMETypeJSON];
这篇关于使用RestKit 0.20.3映射json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!