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

4

1 回答 1

0

您不会自己调用此方法。您的模型类在加载类时调用它一次并获取您的字典映射并在每次初始化模型副本时使用它。

IE。当您调用 initWithDictionary(或 initWithJSONString)时,您的模型类会将每个 JSON 键映射到类属性,但是对于您在 KeyMapper 中列出的键,将执行以下操作:

  • 它将在您的 JSON 中获取潮汐SummaryStats.maxheight 的值并将其存储在您的类模型的“maxheight”属性中
  • 它将采用tideSummaryStats 的值。JSON 中的 minheight 并将其存储在类模型的“minheight”属性中
  • 等等等等

再次注意 - 请注意,您的模型会在初始化时自动为您执行此操作,并且所有其他 JSON 键将尝试映射到具有相应名称的属性。

如果您有 10 分钟的空闲时间,您可以阅读本教程,该教程展示了如何使用自定义键映射器和 YouTube 的 JSON API:

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

于 2013-12-24T07:10:29.757 回答