在Rails 3.0.8中,json包含一个带有模型名称的根元素.例如我的位置模型.
[ { location: { city: San Diego name: Mission Valley YMCA Krause Family Skatepark pads_required: 0 country: United States
并且映射提供程序直接查找位置对象.
RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]]; [locationMapping mapKeyPath:@"id" toAttribute:@"locationId"]; ... [objectManager.mappingProvider setMapping:locationMapping forKeyPath:@"location"];
现在当你升级到rails 3.1.0时,根节点“location”现在默认被删除了,我不知道如何在没有它的情况下配置映射提供程序?我尝试了nil并寻找替代方法,但没有成功.
你知道如何映射吗?请帮忙!
[ { city: San Diego name: Mission Valley YMCA Krause Family Skatepark pads_required: 0 country: United States
解决方法@H_301_16@
从RestKit方面来说,我不知道,但是从
this topic看起来你可以让json回到RestKit期望做的事情:
class Location < ActiveRecord::Base
self.include_root_in_json = true
end
编辑:
为了完整性,以下是使用RestKit的方法:
RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]];
[locationMapping mapKeyPath:@"id" toAttribute:@"locationId"];
...
[objectManager.mappingProvider addObjectMapping:locationMapping];
然后调用mapper:
RKObjectMapping* locationMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[RKLocation class]];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/locations" objectMapping:locationMapping delegate:self];
然后你将处理RKObjectLoader委托方法中的对象.
class Location < ActiveRecord::Base self.include_root_in_json = true end
编辑:
为了完整性,以下是使用RestKit的方法:
RKObjectMapping* locationMapping = [RKObjectMapping mappingForClass:[RKLocation class]]; [locationMapping mapKeyPath:@"id" toAttribute:@"locationId"]; ... [objectManager.mappingProvider addObjectMapping:locationMapping];
然后调用mapper:
RKObjectMapping* locationMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[RKLocation class]]; [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/locations" objectMapping:locationMapping delegate:self];
然后你将处理RKObjectLoader委托方法中的对象.