【问题标题】:dynamic object mapping for nested json in Restkit 0.20Restkit 0.20 中嵌套 json 的动态对象映射
【发布时间】:2014-02-18 23:15:46
【问题描述】:

谁能帮我在restkit 0.20中映射这个json对象?

"feeds" : [
   {
    "id": 32131354,
    "caption": "Blah Blah Blah.",
    "story" : "blah blah someone blah blah someone else",
    "story_tags": {
        "0": [
            {
            "id": 21231654,
            "name": "Someone",
            "offset": 0,
            "length": 25,
            "type": "page"
            }
        ],
        "33": [
            {
            "id": 3213212313,
            "name": "Someone else",
            "offset": 33,
            "length": 12,
            "type": "page"
            }
        ]
    },
}
{
...
}
]

我非常感谢任何帮助。这就是现在正在做的事情:

RKEntityMapping *feedsMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([FacebookFeed class]) inManagedObjectStore:managedObjectStore];
feedsMapping.identificationAttributes = @[@"id"];
[feedsMapping addAttributeMappingsFromDictionary:@{

                                                   @"caption" : @"caption",
                                                   @"id" : @"id",
                                                   @"story" : @"story",
                                                }];

这是用于关系映射

RKEntityMapping *facebookFeedStoryTagsMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([FacebookFeedStoryTag class]) inManagedObjectStore:managedObjectStore];
facebookFeedStoryTagsMapping.identificationAttributes = @[@"id"];
[facebookFeedStoryTagsMapping addAttributeMappingToKeyOfRepresentationFromAttribute:@"offset"];
[facebookFeedStoryTagsMapping addAttributeMappingsFromDictionary:@{
                                                                 @"(offset).id" : @"id",
                                                                 @"(offset).length" : @"length",
                                                                 @"(offset).name" : @"name",
                                                                 @"(offset).type" : @"type",
                                                                 }];

[feedsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"story_tags" toKeyPath:@"story_tags" withMapping:facebookFeedStoryTagsMapping]];

但我收到以下错误:

restkit.object_mapping:RKMappingOperation.m:745 Key path 'story_tags' yielded collection containing another collection rather than a collection of objects: (
    (
            {
        id = 31314654654;
        length = 25;
        name = "Someone";
        offset = 0;
        type = page;
    }
)
)

【问题讨论】:

    标签: objective-c restkit restkit-0.20


    【解决方案1】:

    JSON "0": [ 提出了一个问题,因为我们真的想要一个字典(这就是您的映射的编写方式),但我们实际上有一个数组。这是您看到的错误的来源和含义。

    理想情况下,您应该更改 JSON...

    就目前而言,您的FacebookFeedStoryTag 需要将整个数组(及其包含的字典)映射到可转换的属性中。这是因为映射无法处理嵌套数组。

    您可以将此可转换属性设为瞬态并实现自定义设置器/willSave 方法来获取数组并提取字典内容。由于键名匹配,这可以简单地使用 setValuesForKeysWithDictionary: 完成(尽管您需要处理 offset 键)。

    【讨论】:

    • 我现在正在更改服务器端的 JSON 以克服这个问题,但我想知道是否有一种方法可以直接在对象映射中完成。我想避免在我的服务器端使用额外的 for 循环来更改 JSON。
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    相关资源
    最近更新 更多