2012-06-22 18 views
5

Ho alcuni problemi nel mappare una matrice JSON a RestKit. Ecco come appare il file JSON:Come mappare gli array JSON in RestKit

{"issuelist":[ 
    { 
     "issue":[ 
      { 
       "id":1, 
       "beschreibung":"", 
       "name":"Test1" 
      }, 
      { 
       "id":2, 
       "beschreibung":"", 
       "name":"Test2" 
      } 
     ] 
    } 
]} 

Sono interessato all'array del "problema". Questa è la mia mappatura per un unico problema:

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) { 
     [mapping mapAttributes:@"name", @"beschreibung", nil]; 
     [mapping mapKeyPathsToAttributes: 
       @"id", @"identifier", 
       nil]; 
    }]; 

Ed ecco come ho configurato il mio ObjectMapping

RKObjectMappingProvider *omp = [RKObjectManager sharedManager].mappingProvider; 

RKObjectMapping *issueMapping = [Issue mapping]; 
[omp addObjectMapping:issueMapping]; 

[omp setObjectMapping:issueMapping forKeyPath:@"issuelist.issue"]; 

Purtroppo questo non funziona. Ottengo un output di registro come questo:

 

    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'name'. Transforming from type '__NSArrayI' to 'NSString' 
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'name'. No strategy for transforming from '__NSArrayI' to 'NSString' 
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'name to keyPath 'name' -- value is unchanged ((null)) 
    T restkit.object_mapping:RKObjectMappingOperation.m:322 Mapping attribute value keyPath 'beschreibung' to 'beschreibung' 
    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'beschreibung'. Transforming from type '__NSArrayI' to 'NSString' 
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'beschreibung'. No strategy for transforming from '__NSArrayI' to 'NSString' 
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'beschreibung to keyPath 'beschreibung' -- value is unchanged ((null)) 
    T restkit.object_mapping:RKObjectMappingOperation.m:322 Mapping attribute value keyPath 'id' to 'identifier' 
    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'id'. Transforming from type '__NSArrayI' to 'NSString' 
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'id'. No strategy for transforming from '__NSArrayI' to 'NSString' 
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'id to keyPath 'identifier' -- value is unchanged ((null)) 
    D restkit.object_mapping:RKObjectMappingOperation.m:624 Finished mapping operation successfully... 

Sembra come se RestKit sta cercando di mappare l'intero Arry in una questione invece di creare una serie di questioni. Come devo modificare la mia mappatura per correggere questo?

Grazie per il vostro aiuto!

risposta

9

Prova questa:

RKObjectMapping* issueMapping = [RKObjectMapping mappingForClass: [Issue class] usingBlock:^(RKObjectMapping *mapping) { 
    [mapping mapAttributes:@"name", @"beschreibung", nil]; 
    [mapping mapKeyPathsToAttributes: 
    @"id", @"identifier", 
    nil]; 
}]; 
issueMapping.rootKeyPath = @"issue"; 
[omp setObjectMaping: issueMapping forKeyPath: @"issuelist"]; 

Questo dice, quando issuelist percorso chiave viene rilevato l'uso del issueMapping. E poi dice per ogni problema di root, creare un oggetto Issue.

+0

Grazie! E 'stato utile! – thalador