Restkit OM2关系映射
我使用 Restkit OM2 接收 json 并映射到 iphone 上的对象。 我目前对如何构建映射感到困惑,并且需要一些帮助。
下面是一个示例 json 文件,
{
-magic_verbs: [
-{
lemma: "work"
position: 5
score: "0.75"
value: "working"
}
-{
lemma: "head"
position: 0
score: "0.75"
value: "heading"
}
],
magic_advs: [
-{
lemma: "not"
position: 2
score: "0.6"
value: "not"
}
-{
lemma: "just"
position: 2
score: "0.6"
value: "just"
}
]
}
我只需要每个文件中的引理和值字段。例如,动词类包含,
@interface Verbs : NSManagedObject {
}
@property (nonatomic,retain) NSString *lemma;
@property (nonatomic,retain) NSString *value;
@end
@implementation Verbs
@synthesize lemma,value;
@end
然后我读取 json 并使用下面的代码创建映射,
objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKRelationshipMappingExample.sqlite"];
[RKObjectManager setSharedManager:objectManager];
RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];
RKObjectMapping* verbMapping = [RKObjectMapping mappingForClass:[Verbs class]];
[verbMapping mapKeyPath:@"lemma" toAttribute:@"lemma"];
[verbMapping mapKeyPath:@"value" toAttribute:@"value"];
[provider setMapping:verbMapping forKeyPath:@"magic_verbs"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/users/1/magic_words" objectMapping:verbMapping delegate:self];
我对 adjs 映射做了同样的事情。我还有一个名为 Word 的类,其中包含 2 个 nsarray,用于包含对象映射数据。但我不确定如何实现这一点并将它们正确链接起来。
@interface Words : NSObject {
NSArray *_verbs;
NSArray *_adjs;
}
@property (nonatomic, retain) NSArray *verbs,*adjs;
@end
对此的任何帮助和指导表示赞赏。我查看了目录项目中的示例,并且能够运行它,但无法掌握将其应用到我自己的 json 文件的概念。
谢谢
G
Im using Restkit OM2 to take in a json and map to objects on iphone.
Im currently confused on how to structure the mappings and could do with some help.
Below is an example json file
{
-magic_verbs: [
-{
lemma: "work"
position: 5
score: "0.75"
value: "working"
}
-{
lemma: "head"
position: 0
score: "0.75"
value: "heading"
}
],
magic_advs: [
-{
lemma: "not"
position: 2
score: "0.6"
value: "not"
}
-{
lemma: "just"
position: 2
score: "0.6"
value: "just"
}
]
}
i only need the lemma and value fields from each of these. so for example the verb class contains
@interface Verbs : NSManagedObject {
}
@property (nonatomic,retain) NSString *lemma;
@property (nonatomic,retain) NSString *value;
@end
@implementation Verbs
@synthesize lemma,value;
@end
then i read in the json and create the mappings with below code
objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKRelationshipMappingExample.sqlite"];
[RKObjectManager setSharedManager:objectManager];
RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];
RKObjectMapping* verbMapping = [RKObjectMapping mappingForClass:[Verbs class]];
[verbMapping mapKeyPath:@"lemma" toAttribute:@"lemma"];
[verbMapping mapKeyPath:@"value" toAttribute:@"value"];
[provider setMapping:verbMapping forKeyPath:@"magic_verbs"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/users/1/magic_words" objectMapping:verbMapping delegate:self];
I have done the same for the adjs mapping. I also have a class called words that contains 2 nsarrays that is to contain the object mapping data. but im unsure how to implement this and link them up correctly.
@interface Words : NSObject {
NSArray *_verbs;
NSArray *_adjs;
}
@property (nonatomic, retain) NSArray *verbs,*adjs;
@end
any help and guidance on this is appreciated. I have looked at the example in catalog project and have been able to get that running but havent been able to master the concept to apply it to my own json files.
thanks
G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您查看 OM 2.0 的新文档,该文档位于 Github 页面此处
如果你还没有注意到。它清楚地说明了如何将 JSON 映射到对象。
I suggest you to look at the new documentation of OM 2.0 that is in the Github page here
if you haven't noticed yet. It clearly lays out on how to map your JSON to an object.