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
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:
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/