【问题标题】:Hard time mapping the JSON in restkit很难在 restkit 中映射 JSON
【发布时间】:2012-12-24 16:02:29
【问题描述】:

我很难将 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];

谁能帮我解决这个问题。我在核心数据的映射上苦苦挣扎了好几天。

亲切的问候

【问题讨论】:

    标签: ios objective-c json mapping restkit


    【解决方案1】:

    尝试为此使用 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",
        }
        }]
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2012-06-24
      • 2014-06-06
      • 2012-12-22
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多