JSONModel used with Objects/Dictionaries

2.6k Views Asked by At

Ive been working with JSONModel, the tutorials are making sense. They are parsing JSON contain an array with multiple indexes.

I wanted to make sure JSONModel allowed to be used with say this dataset: https://gist.github.com/ryancoughlin/8043604

tide is not an array correct? But there is tide.tideSummary - which contains array of tide data across multiple days.

AllTide.h

#import "JSONModelLib.h"
#import "Tide.h"

@interface AllTide : JSONModel

@property (strong,nonatomic) NSDictionary<Tide> *tide;

@end

Tide.h

#import "JSONModelLib.h"
#import "TideSummaryStats.h"

@protocol Tide @end

@interface Tide : JSONModel

@property (nonatomic, strong) NSArray<TideSummaryStats> *tideSummaryStats;

@end

TideSummaryStats.h

#import "JSONModelLib.h"

@protocol TideSummaryStats @end

@interface TideSummaryStats : JSONModel

@property (nonatomic, strong) NSString *maxheight;
@property (nonatomic, strong) NSString *minheight;

@end

TideDetailViewController - Displays a single location (detail view) vs a list of multiple locations

@interface TideDetailViewController () {
    AllTide *_objTide;
}

@end

@implementation TideDetailViewController

- (void)viewDidAppear:(BOOL)animated {

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

    //fetch the feed
    _objTide = [[AllTide alloc] initFromURLWithString:locationQueryURL                                         completion:^(JSONModel *model, JSONModelError *err) {

                                             NSLog(@"Tides: %@", _objTide.tide);

                                         }];
}

Been going through several JSONModel tutorials and it makes sense, I think I am having trouble where my JSON format differs from the tutorials. Again, where my tide does not return an array.

Would this be a good case to utilize JSONModel keymapper?

Any ideas? Let me know if I can provide anything else. Been diving around for some guidance, but a bit stuck. Thanks in advance!

1

There are 1 best solutions below

2
On

you don't need AllTide.h

try this:

TideDetailViewController - Displays a single location (detail view) vs a list of multiple locations

@interface TideDetailViewController () {
  NSArray *arrTide;
}

@end

@implementation TideDetailViewController

- (void)viewDidAppear:(BOOL)animated {

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

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

    arrTide = [TideSummaryStats arrayOfModelsFromDictionaries:json[@"tide"][@"tideSummaryStats"]                                                ];

     NSLog(@"Tides: %@", arrTide[0]);

  }];
}