RestKit mapKeyPath 到数组索引
我想使用 RestKit (OM2) 将给定的数组索引映射到属性中。我有这个 JSON:
{
"id": "foo",
"position": [52.63, 11.37]
}
我想将其映射到这个对象:
@interface NOSearchResult : NSObject
@property(retain) NSString* place_id;
@property(retain) NSNumber* latitude;
@property(retain) NSNumber* longitude;
@end
我不知道如何将 JSON 中的位置数组中的值映射到 Objective-C 类的属性中。到目前为止,映射看起来像这样:
RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]];
[resultMapping mapKeyPath:@"id" toAttribute:@"place_id"];
现在如何添加纬度/经度的映射?我尝试了各种方法,但不起作用。例如:
[resultMapping mapKeyPath:@"position[0]" toAttribute:@"latitude"];
[resultMapping mapKeyPath:@"position.1" toAttribute:@"longitude"];
有没有办法将 JSON 中的 position[0]
映射到我的对象中的 latitude
?
I would like to map a given array index into a property with RestKit (OM2). I have this JSON:
{
"id": "foo",
"position": [52.63, 11.37]
}
which I would like to map to this Object:
@interface NOSearchResult : NSObject
@property(retain) NSString* place_id;
@property(retain) NSNumber* latitude;
@property(retain) NSNumber* longitude;
@end
I can't figure out how to map values out of the position array in my JSON into properties of my objective-c class. The mapping looks like this so far:
RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]];
[resultMapping mapKeyPath:@"id" toAttribute:@"place_id"];
Now how can I add a mapping for the latitude/longitude? I tried various things, that don't work. e.g.:
[resultMapping mapKeyPath:@"position[0]" toAttribute:@"latitude"];
[resultMapping mapKeyPath:@"position.1" toAttribute:@"longitude"];
Is there a way to map position[0]
out of the JSON into latitude
in my object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是否定的 - 键值编码不允许这样做。对于集合,仅支持 max、min、avg、sum 等聚合操作。
您最好的选择可能是将 NSArray 属性添加到 NOSearchResult:
并像这样定义映射:
之后,您可以从坐标手动分配纬度和经度。
编辑:进行纬度/经度分配的好地方可能是在对象加载器委托中
,
The short answer is no - key-value coding doesn't allow for that. For collection, only aggregate operations such as max, min, avg, sum are supported.
Your best bet is probably to add an NSArray property to NOSearchResult:
and define mapping like this:
After that, you could manually assign latitude and longitude from coordinates.
EDIT: A good place to do latitude/longitude assignment is probably in object loader delegates
and