How to map a JSON array to RestKit

I have a json string similar to this format:

[{"image":"/0001.jpg","link":"/index.php"},
{"image":"/0001.jpg","link":"/index.php"}]

he does not have a key at the top level.

[mapping mapKeyPath:@"image" toAttribute:@"image"];

a display like this will not work, it will give me an error:

restkit.object_mapping:RKObjectMapper.m:81 Adding mapping error: Could not find an object mapping for keyPath: ''

How to match this json type?

+5
source share
2 answers

Using

[[RKObjectManager sharedManager].mappingProvider addObjectMapping:myObject];

You should check the “ Non-KVC Mapping ” section of the “Documents for Binding Backup Objects” section.

Here is an example from the docs:

[
    { "title": "RestKit Object Mapping Intro",
      "body": "This article details how to use RestKit object mapping...",
      "author": {
          "name": "Blake Watters",
          "email": "blake@restkit.org"
      },
      "publication_date": "7/4/2011"
    }
]

And you map it like this:

// Our familiar articlesMapping from earlier
RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class]];
[articleMapping mapKeyPath:@"title" toAttribute:@"title"];
[articleMapping mapKeyPath:@"body" toAttribute:@"body"];
[articleMapping mapKeyPath:@"author" toAttribute:@"author"];
[articleMapping mapKeyPath:@"publication_date" toAttribute:@"publicationDate"];

[[RKObjectManager sharedManager].mappingProvider addObjectMapping:articleMapping];
+1
source

For me, the solution was:

add this response descriptor, use the object inside the array

[manager addResponseDescriptor:[ObjectInsideTheArray getResponseDescriptor]];

and in response to success

NSArray * response = (NSArray *) [mappingResult array ]; // instead of firstObject

0
source

All Articles