我使用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映射做了同样的事情。我还有一个名为words的类,它包含2个nsarray,用于包含对象映射数据。但我不确定如何实现这一点并正确链接它们。
@interface Words : NSObject {
NSArray *_verbs;
NSArray *_adjs;
}
@property (nonatomic, retain) NSArray *verbs,*adjs;
@end
对此有任何帮助和指导表示赞赏。我已经查看了目录项目中的示例,并且已经能够运行但未能掌握该概念以将其应用于我自己的json文件。
感谢