Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a JSON array with RestKit

Tags:

json

ios

restkit

I have json string like this format :

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

it does not have a key in the top level.

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

mapping like this won't work , it give me the error:

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

How to map this type of json ?

like image 308
kran Avatar asked Nov 14 '22 04:11

kran


1 Answers

Use

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

You should check "Mapping without KVC" section on Restkit Object Mapping Docs.

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": "[email protected]"
      },
      "publication_date": "7/4/2011"
    }
]

And you map that 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];
like image 181
clopez Avatar answered Jan 07 '23 16:01

clopez