Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe you miss one 'level' of your mapping. If you take a closer look at your input, you have an dictionary (key1), which contents is not single '<code>Sendung</code>' but an array of such objects. Thus, you can't map contents of the key1 directly to <code>Sendung</code> ivars, rather than create new class, eg <code>SendungResult</code>.</p> <pre><code>@interface SendungResult : NSObject @property (nonatomic, strong) NSString *keyName; @property (nonatomic, strong) NSArray *sendungs; @end </code></pre> <p>and map the contents of key1 to <code>SendungResult</code> <code>sendungs</code> array using the relationship mapping you use now directly.</p> <p><strong>EDIT:</strong> I'm extending the answer as follows:</p> <p>As i said earlier, you are trying to map a collection of objects into a single instance, so this can not work. Instead, you have to create a <code>SendungResult</code> that will hold an array of <code>Sendung</code> instances for one key. So in your example, you'll end up with two <code>SendungResult</code> instances (one will hold three <code>Sendung</code>s - those nested under <code>"key1"</code> and the second one just two, nested under <code>"key2"</code>). You can't map this kind of a structure into just once class, you have to nest them same way as your JSON is nested. The <code>Sendung</code> class is basically unchanged. </p> <p>Now for the mapping. You need to define two mappings, one will map the contents of your root JSON into two <code>SendungResult</code> (one for each key) and the second mapping will map the inner part into individual <code>Sendung</code> instances.</p> <p>We start with the inner mapping.</p> <pre><code>RKObjectMapping *sendungMapping = [RKObjectMapping mappingForClass:[Sendung class]]; [sendungMapping mapKeyPath:@"desc" toAttribute:@"desc"]; [sendungMapping mapKeyPath:@"duration" toAttribute:@"duration"]; [[[RKObjectManager sharedManager] mappingProvider] addObjectMapping:sendungMapping]; </code></pre> <p>Nothing really interesting here, we just map the keypaths to your properties. Now for the outer part.</p> <pre><code>RKObjectMapping *resultMapping = [RKObjectMapping mappingForClass:[SendungResult class]]; [resultMapping setForceCollectionMapping:YES]; [resultMapping mapKeyOfNestedDictionaryToAttribute:@"keyName"]; [resultMapping mapKeyPath:@"(keyName)" toRelationship:@"sendungs" withMapping:sendungMapping]; [[[RKObjectManager sharedManager] mappingProvider] addObjectMapping:resultMapping]; </code></pre> <p>We tell RestKit to map contents of the keyName keyPath to <code>sendungs</code> property found on <code>SendungResult</code> class with the mapping defined earlier.</p> <p>Now we can fetch the data </p> <pre><code>[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"test.js" objectMapping:resultMapping delegate:self]; </code></pre> <p>The RestKit will create two <code>SendungResult</code> instances, each array contains the inner <code>Sendung</code> classes (3 and 2, respectively)</p> <p><strong>Note:</strong> I strongly suggest you to read <a href="https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md" rel="noreferrer">RestKit Object Mapping</a> documentation. Also, JSONLint found your JSON to be invalid, i had to remove the semicolons. So be sure to use valid json.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload