使用 Restkit 以 json 形式发送整数
现在我正在使用 Restkit 从 iPhone 设备向远程服务器发出 REST 和 RPC 请求,这是一个非常棒的库。
现在我遇到了一个简单的问题。我的服务器需要一些整数数据。这意味着 json 数组看起来像这样。
{'myInteger':1234}
请注意,my_integer_field 键的值不在“”之间。这意味着该值是数字而不是字符串。
当我进行映射和所有内容时,我将 my_integer_field 设置为对象定义中的 NSNumber 值。但是当对象被解析为 Json 时,值出现在 '' 之间,例如:
{'myInteger':'1234'}
因此,我收到服务器响应的错误,因为假设 '1234' 代表字符串而不是数字。有没有办法强制 json 解析在 NSNumber 字段上不包含 '' ?
更新
这是代码:
//myclass.h
@interface myClass : NSObject {
NSNumber *myInteger;
}
@property (nonatomic, retain) NSNumber *myInteger;
//appDelegate.m here is where i define the mappings for everything.
//myClass MAPPING
RKObjectMapping* myClassMapping = [RKObjectMapping mappingForClass:[myClass class]];
[myClassMapping mapAttributes:@"myInteger", nil];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:myClassMapping];
// myClass SERIALIZATION
RKObjectMapping* myClassSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[myClassSerializationMapping mapAttributes:@"myInteger", nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:myClassSerializationMapping forClass:[myClass class]];
我遗漏了什么?
Right now I'm working with Restkit to make REST and RPC request to a remote server from the iPhone device, it's a pretty awesome library.
Right now I got stuck with a simple problem. My server expects some data as integer. That means a json array that looks like this.
{'myInteger':1234}
Please notice that the value for my_integer_field key is not between ''. That means that the value is a number and not a string.
When I make the mappings and all the stuff I set my_integer_field as a NSNumber value in the object definition. But when the object is parsed to Json the value appears between '' like:
{'myInteger':'1234'}
So I get an error as response from the server because is asuming that '1234' represents a String and not a number. Is there a way to force the json parsing to not include '' on NSNumber fields?
UPDATE
Here is the code:
//myclass.h
@interface myClass : NSObject {
NSNumber *myInteger;
}
@property (nonatomic, retain) NSNumber *myInteger;
//appDelegate.m here is where i define the mappings for everything.
//myClass MAPPING
RKObjectMapping* myClassMapping = [RKObjectMapping mappingForClass:[myClass class]];
[myClassMapping mapAttributes:@"myInteger", nil];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:myClassMapping];
// myClass SERIALIZATION
RKObjectMapping* myClassSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[myClassSerializationMapping mapAttributes:@"myInteger", nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:myClassSerializationMapping forClass:[myClass class]];
I'm missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定为什么会出现这种行为,但是您是否尝试过仅使用:
RKObjectMapping* myClassSerializationMapping = [myClassMapping inverseMapping];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:myClassSerializationMapping forClass:[myClass 类]];
虽然我相信如果没有指定serializationMapping,RestKit 默认使用 inverseMapping,所以你可能可以完全忽略该部分。
I'm not precisely sure why you get this behavior, but have you tried just using:
RKObjectMapping* myClassSerializationMapping = [myClassMapping inverseMapping];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:myClassSerializationMapping forClass:[myClass class]];
Although I believe that RestKit uses inverseMapping by default if no serializationMapping is specified, so you might be able to just leave off that part entirely.