ios - 在 Restkit 中映射 JSON 很困难

标签 ios objective-c json mapping restkit

我很难将 JSON 映射到我的 RestKit 应用程序中。我也想将它保存在核心数据中。因此这里有两个图像链接。 第一张图片,我的 JSON 是什么样子的。

IMAGE1

第二张图片是我的核心数据库的样子。

IMAGE2

现在,我首先制作了没有核心数据层的应用程序。因此,只需读取数据并使用它即可。那时一切都正常了。但使用核心数据时,我遇到了一些像这样的映射错误。

日志

 2013-01-10 10:46:02.663 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Person'
2013-01-10 10:46:02.664 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Function'
2013-01-10 10:46:02.664 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Department'
2013-01-10 10:46:02.665 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Company'

2013-01-10 10:46:02.672 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'user' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.673 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'function' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.674 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'department' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.676 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'company' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.

这就是我在代码中进行关系映射的方式。

 RKRelationshipMapping* relationUserMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Person"toKeyPath:@"user"withMapping:personMapping];
    RKRelationshipMapping* relationFunctionMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Function"toKeyPath:@"function"withMapping:functionMapping];
    RKRelationshipMapping* relationDepartmentMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Department"toKeyPath:@"department"withMapping:departmentMapping];
    RKRelationshipMapping* relationCompanyMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Company"toKeyPath:@"company"withMapping:companyMapping];
      NSLog(@"till here7");
    [dataMapping addPropertyMapping:relationUserMapping];
    [dataMapping addPropertyMapping:relationFunctionMapping];
    [dataMapping addPropertyMapping:relationDepartmentMapping];
    [dataMapping addPropertyMapping:relationCompanyMapping];

任何人都可以帮我解决这个问题吗?我现在在核心数据的映射上苦苦挣扎了好几天。

亲切的问候

最佳答案

尝试使用 RKManagedObjectMapping 来实现此目的。配置 RKManagedObjectMapping 对象如下。

RKURL *baseURL = [RKURL URLWithBaseURLString:serverUrl];

    RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];

    objectManager.client.baseURL = baseURL;

    objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;

    objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];

    RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:DB_NAME];

    objectManager.objectStore = objectStore;

然后,您必须为每个数据类及其所具有的关系设置映射。我只是尝试在这里放一个我用过的例子。

    /** specify the base mapping of reservation class */
    RKManagedObjectMapping *getReservationMapping = [RKManagedObjectMapping mappingForClass:[Reservation class] inManagedObjectStore:objectManager.objectStore];
    /** generate mapping for Reservation class */
    [self setReservationMapping:getReservationMapping];
    getReservationMapping.primaryKeyAttribute=@"reservationId";
    [objectManager.mappingProvider setMapping:getReservationMapping forKeyPath:@""];


    /** specify the base mapping of ReservationsType class */
    RKManagedObjectMapping *resTypeMapping = [RKManagedObjectMapping mappingForClass:[ReservationsType class] inManagedObjectStore:objectManager.objectStore];
    /** generate mapping for ReservationsType class */
    [self setReservationsTypeMapping:resTypeMapping];
    resTypeMapping.primaryKeyAttribute=@"reservationTypeId";
    [getReservationMapping mapRelationship:@"reservationsType" withMapping:resTypeMapping];
    [objectManager.mappingProvider setMapping:resTypeMapping forKeyPath:@".reservationsType"];

我的 setReservationMapping 如下

    - (void)setReservationMapping:(RKManagedObjectMapping *)object
    {
     [object mapKeyPathsToAttributes:
       @"id", @"reservationId",
       @"comment", @"comment",
       @"date", @"date",
       @"email", @"email",nil];
    }

这是我的 JSON 示例。请注意,我的预留节点没有标识符,它是空白的,因此我给出了 keyPath:@""。

     [{
    id: 5644,
    comment: "",
    email: "danish@abc.com",
    firstName: "Danny",
    reservationsType: 
{
    id: 1,
    name: "Reservation",
    }
    }]

希望对你有帮助。

关于ios - 在 Restkit 中映射 JSON 很困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14255008/

相关文章:

ios - AVPlayer 不播放 .mov 文件

javascript - GAS 中的 Access-Control-Allow-Origin header

mysql - 如何使用 MariaDB 从 JSON 数组中的多个对象中提取值?

iphone - 如何在我的应用程序中纠正与 iAd 相关的崩溃

iphone - 设备总下载和上传数据

iphone - 在 iOS 中多次播放声音的正确方法

objective-c - 为什么在 obj-c 中调用该方法时未实现的可选协议(protocol)方法会导致运行时错误?

objective-c - 全局鼠标事件和全屏应用程序

objective-c - 如何从旨在支持 COCOA 下构建的 GUI 的主 NSWindow 控制位于专用 NSWindow 中的 NSView?

json - 将 JSON 数组解码为 JSON 对象