0

I am using JSONModel for a basic app that returns a JSON object.

Here is a sample of the data I am returning: https://gist.github.com/ryancoughlin/8043604 - Focusing on the tide object.

I am trying to work in JSONModel JSONKeyMapper - docs here (scroll down towards the middle) - https://github.com/icanzilb/JSONModel/blob/master/README.md#magical-data-modelling-framework-for-json

I am trying to find out how to implement it. I understand that it takes a key path similar too:


EDIT: From my breakpoint: http://dl.dropbox.com/u/19301636/Screenshots/rzqv.png

This is what json returns: http://dl.dropbox.com/u/19301636/Screenshots/y5mt.png


TIDEMAPPER.M

#import "TideMapper.h"

@implementation TideMapper

+(JSONKeyMapper*)keyMapper
{
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"tide.tideInfo": @"tideSite",
                                                       @"tide.tideSummaryStats": @"maxheight",
                                                       }];
}

@end

TIDEMAPPER.M

#import "TideMapper.h"

@implementation TideMapper

+(JSONKeyMapper*)keyMapper
{
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"tideSummaryStats.maxheight": @"maxheight",
                                                       @"tideSummaryStats.minheight": @"minheight",
                                                       @"tideInfo.lat": @"lat",
                                                       @"tideInfo.lon": @"lon"
                                                       }];
}

@end

VIEW

@implementation TideDetailViewController

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:YES];

    NSString *locationQueryURL = @"http://api.wunderground.com/api/xxxx/tide/geolookup/q/43.5263,-70.4975.json";

    [JSONHTTPClient getJSONFromURLWithString: locationQueryURL
                                  completion:^(NSDictionary *json, JSONModelError *err) {

//                                      NSArray* results = [json valueForKeyPath:@"tide.tideInfo"];

                                      _tide = [TideMapper arrayOfDictionariesFromModels:json];
                                       NSLog(@"loans: %@", _tide);

                                  }];
}

I think I need to change tide to NSDictionary - I dont think it returns an array. Its only a single result for a location

I am stuck when it comes to calling this method. Does anyone have any experience using this JSONModel KeyMapper?

Thanks

1 Answers1

0

You do not call this method yourself. Your model class calls it once when the class is loaded and gets your dictionary map and uses it every time you initialize a copy of your model.

Ie. when you call initWithDictionary (or initWithJSONString) your model class will map each JSON key to a class property, however for the keys you listed in your KeyMapper will do the following:

  • it will take the value of tideSummaryStats.maxheight in your JSON and store it in your class model's 'maxheight' property
  • it will take the value of tideSummaryStats. minheight in your JSON and store it in your class model's 'minheight' property
  • etc, etc.

Again - note that your model will automatically do that for you upon init, and all other JSON keys it'll try to map to properties with their corresponding names.

If you have 10 spare minutes you can read through this tutorial, which showcases using a custom key mapper with YouTube's JSON API:

http://www.touch-code-magazine.com/how-to-make-a-youtube-app-using-mgbox-and-jsonmodel/

Marin Todorov
  • 6,377
  • 9
  • 45
  • 73
  • Ok thanks, I was guessing that it would map to each property defined. I went through that YT tutorial and also the Kiva feed one. I looked specifically at the YT tutorial you posted to to see how the key mapper worked. Would I access them by ```tide.maxheight``` if I was setting something like this ```tide = [TideMapper arrayOfModelsFromDictionaries:json[@"tide"]];``` –  Dec 24 '13 at 16:20
  • If ```arrayOfModelsFromDictionaries``` takes a ```NSArray``` - and I need to pass it a ```NSDictionary``` from my returned JSON (posted in my question). I wont be able to use that method. Right? –  Dec 24 '13 at 16:22
  • arrayOfModelsFromDictionaries takes an NSArray parameter, why would you pass it an NSDictionary? – Marin Todorov Dec 29 '13 at 09:07
  • I should reword. I was seeing if it was possible to convert my dictionary to an array pass to ```arrayOfModelsFromDictionaries``` - then I would be able to build my model based on that. Since my first level ```tide``` does not return an array. I thought the ```keyMapper``` would be a good fit because it would help pick and match the items that I need to extract from my JSON. –  Dec 29 '13 at 13:37
  • I would say it's not possible – Marin Todorov Jan 09 '14 at 17:58