【问题标题】:RestKit - saving JSON content into a string coredata attributeRestKit - 将 JSON 内容保存到字符串 coredata 属性中
【发布时间】:2016-02-20 04:26:38
【问题描述】:

我需要解析一个 JSON Web 服务响应,其中包含一个子项未知的键。

例如,让我们有以下 JSON 响应,其中 customData 属性的键是在运行时定义的:

{
    "score": 996,
    "customData": { "key1": "key1value", "key2": "key2value" },
    "isRegistered": true,
    "allowOpening": "OK"
}

是否可以将 customData 的 JSON 内容保存到字符串 coredata 属性中?

我尝试过这样一个简单的映射:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:[[self class] description] inManagedObjectStore:managedObjectStore];
[mapping addAttributeMappingsFromDictionary:@{
                 @"score": @"score",
                 @"customData":@"customData",
                 @"isRegistered":  @"isRegistered",
                 @"allowOpening": @"allowOpening"}];

但是不行,coredata保存的customData一直是空的。

非常感谢, 丹

【问题讨论】:

    标签: ios core-data mapping restkit


    【解决方案1】:

    是否可以将 customData 的 JSON 内容保存到字符串 coredata 属性中?

    不,因为它将被反序列化为字典,并且没有将其转换为字符串的转换器。

    您可以将其存储为字典。您可以添加一个带有动态映射的关系映射,该映射检查键是什么并动态定义映射...

    【讨论】:

    • 谢谢 Wain,动态映射可能是一种解决方案,但我不认为 CoreData 模型可以在运行时更改。
    • 确实不行。只有当你有未知的源密钥和已知的目标密钥,并且源密钥来自一组已知的类似选项时,这才有效。
    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 2020-01-02
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多