RestKit RKObjectMapping.. Nested & throwaway objects

107 views Asked by At

So, I have an api call that'll output some raw json in the format below...

A bit messy, but I'm only interested in the Mapping portion of this json. If I had access to the raw data, I'd serialise the json cut to the dictionary / array I want to iterate through.. However the client is using restkit and this doesn't seem quite as easy as I'm imagining it....

Also What should I do with the Images section? Again, if was doing this with serialised json, I could open up the dictionary and set what's inside to an object with name description etc... can I do this as well?

{
  "rsp": {

       sn         : "SerialNumber",
       name       : "Service Name",
       from       : "2000-01-01T00:00:00.000+01:00",
       to         : "2000-01-02T00:00:00.000+01:00",
       mappings : {

           "mapping" : {

              "1" : {

                      from     :    2000-06-01T00:01:00.000+01:00
                      to       :    2000-06-01T01:02:00.000+01:00
                      content  : {

                         name        : "name"
                         description : "description"
                         images : {
                            image : "b47ab5a8.png"
                         }
                      }
               },
              "2" : {

                      from     :    2000-06-01T00:01:00.000+01:00
                      to       :    2000-06-01T01:02:00.000+01:00
                      content  : {

                         name        : "name"
                         description : "description"
                         images : {
                            image : "b47ab5a8.png"
                         }
                      }
               },

               // etc...

My question is

 // what goes here

 RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[MYItem class]];

[itemMapping addAttributeMappingsFromDictionary:@{@"description":@"itemDescription",
                                 @"name":@"name"}];

And how do I add the image to MYItem?

1

There are 1 answers

0
Wain On BEST ANSWER

For the image your mapping is something like:

[itemMapping addAttributeMappingsFromDictionary:@{@"description":@"itemDescription",
                               @"name":@"name",
                               @"images.image":@"image"}];

i.e. key paths work But, not that they only work for dictionaries. You can't index into arrays arbitrarily in the same way.

The key path is also in the response descriptor and that's how you drill down to the mapping, i.e. rsp.mappings.mapping (again, because it's dictionaries).