2013-01-10 48 views
0

我很難將JSON映射到我的RestKit應用程序中。我想將它保存在覈心數據中。因此這裏有兩個圖像鏈接。 第一張圖片,我的JSON是怎樣的。硬件時間映射restkit中的JSON

IMAGE1

2圖像是我的核心數據庫的樣子。

IMAGE2

現在第一I由不含芯數據層應用程序。所以只需讀取數據並使用它。在那一刻,一切都奏效了。但是對於核心數據,我得到了一些像這樣的映射錯誤。

LOG

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]; 

任何人都可以幫助我這個。我在覈心數據映射方面掙扎了好幾天。

親切的問候

回答

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]; 
    [email protected]"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]; 
    [email protected]"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: "[email protected]", 
    firstName: "Danny", 
    reservationsType: 
{ 
    id: 1, 
    name: "Reservation", 
    } 
    }] 

希望這可以幫助你。