Restkit OM2关系映射

发布于 2024-11-17 03:21:12 字数 1892 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-11-24 03:21:12

我建议您查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文