0

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
    hey Coughlin why you are using third party lib for json – kamalesh kumar yadav Dec 28 '13 at 15:25
  • I thought it would help streamline, and organize classes based on the JSON I wanted to display. –  Dec 28 '13 at 15:41
  • 1
    simply use make asyncronus call to url and use jsonserilization to handle response data,all this are avalible in native apple. – kamalesh kumar yadav Dec 28 '13 at 15:45
  • @kamaleshkumaryadav Some of the items are nested JSON. Example: ```tide.tideSummary``` -- ```tide.tideSummaryStats``` -- ```tide.tideInfo``` - Did you see the returned data by any chance? –  Dec 28 '13 at 15:52

1 Answers1

0

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]);

  }];
}
Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26
  • Then if I wanted to access "maxheight" - how would I access that? ```objectForKey``` ? And then convert to a string and assign to my label text? –  Dec 28 '13 at 17:58
  • arrTide is an array of TideSummaryStats, so declare for example `TideSummaryStats *t = arrTide[0];` and test a log for "maxheight" `NSLog(@"Tide Max Height: %@", [t maxheight]);` – Jérôme Teisseire Dec 28 '13 at 18:48