1

I came across few posts here related to what I am doing but I am working with some nested objects that I want to extract.

This is a sample of my returned data - https://gist.github.com/ryancoughlin/8043604

I have this in my header so far :

#import "TideModel.h"

@protocol TideModel
@end

@implementation TideModel

-(id)initWithDict:(NSDictionary *)json {
    self = [super init];
    if(self) {
        self.maxheight = [dictionary valueForKeyPath:@"tide.tideSummaryStats.minheight"];
        self.minheight = [dictionary valueForKeyPath:@"tide.tideSummaryStats.maxheight"];
        self.tideSite = [dictionary valueForKeyPath:@"tide.tideInfo.tideSite"];
    }
    return self;
}
@end

I have declared a property for each string and i am accessing it accordingly.
But what I have above doesn't work, maybe because it wont know what to drill in to correct?... Or will it?

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
  • `tide.tideSummaryStats` returns an array. infact even `tide.tideInfo` returns an array. also, it shouldn't be `[dictionary valueForKeyPath:...]` it should be `[json valueForKeyPath:...]` – staticVoidMan Dec 28 '13 at 19:32

2 Answers2

0
  1. tide.tideSummaryStats returns an array.
  2. tide.tideInfo returns an array.

So you can't do -valueForKeyPath: all the way.


Also, this is incorrect: [dictionary valueForKeyPath:...];
it should be : [json valueForKeyPath:...];

because json is the name of the NSDictionary variable passed (not dictionary)


Try this (not sure):

-(id)initWithDict:(NSDictionary *)json {
    self = [super init];

    if(self) {
        NSArray *arrOfTideSummaryStats = [json valueForKeyPath:@"tide.tideSummaryStats"];
        NSDictionary *dctOfTideSummaryStats = [arrOfTideSummaryStats objectAtIndex:0];

        //since self.maxheight and self.minheight are NSString objects and 
        //the original keys "minheight" & "maxheight" return float values, do:
        self.maxheight = [NSString stringWithFormat:@"%f", [dctOfTideSummaryStats valueForKey: @"maxheight"]];
        self.minheight = [NSString stringWithFormat:@"%f", [dctOfTideSummaryStats valueForKey: @"minheight"]];

        /*============================================================*/

        NSArray *arrOfTideInfo = [json valueForKeyPath:@"tide.tideInfo"];
        NSDictionary *dctOfTideInfo = [arrOfTideInfo objectAtIndex:0];

        self.tideSite = [dctOfTideInfo valueForKey:@"tideSite"];
    }

    return self;
}

Similar Questions:

Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • As more items grow, I would organize them in here, or is it best practice to continue breaking them up? (or does it depend more on the case and the type of data?) If you see ```summaryStats``` is an array with days ill want to display. –  Dec 29 '13 at 04:46
  • @Coughlin : depends on the case but basically, if you had only nested dictionaries then KVC is of great help but when there's an array (_with multiple indexes_) in between, then KVC doesn't work so well. [check: link](http://www.cocoabuilder.com/archive/cocoa/307498-is-it-possible-to-traverse-dictionaries-and-arrays-using-kvc.html) (it's quite old, 2011 and i am not sure if things are any different now) – staticVoidMan Dec 29 '13 at 09:19
  • @Coughlin : added one more link in the answer that may be of some help to you. [check: link](http://stackoverflow.com/questions/12054846/keypath-for-first-element-in-embedded-nsarray) – staticVoidMan Dec 29 '13 at 09:35
  • Ok, great. Reading up on that now. Would that be a good use of a framework such as JSONModel? In conjunction to the code above to display key values? –  Dec 29 '13 at 13:31
  • I tried it do display ALL my data, but I think it was overkill since it looks primarily for arrays. –  Dec 29 '13 at 13:32
  • @Coughlin : True, it is overkill but that's the issue with an array and KVC (_correct me if i am wrong_). And... Yes, from the looks of what you're trying to do, `JSONModel` would be of immense help (_you just need to link everything correctly_) : check -- [JSONModel](https://github.com/icanzilb/JSONModel), [Jastor](https://github.com/elado/jastor), [iOS JSON Tools of 2012](http://www.sundoginteractive.com/sunblog/posts/ios-json-tools-making-it-easy) – staticVoidMan Dec 30 '13 at 05:44
0

Recently had to create a app that worked with a remote RESTful server that returned JSON data and was then deserialised into an object for graphing.

I used unirest for the requests and responses and then deserialised the returned JSON into an object. Below is an extract of the code where "hourlySalesFigures" within dictionary "jsonResponseAsDictionary" was a JSON collection of 24 figures which I put into an array. Please note the function is a lot larger but I removed anything which I thought was distracting.

- (PBSSales*) deserializeJsonPacket2:(NSDictionary*)jsonResponseAsDictionary withCalenderType:(NSString *)calendarViewType
{
    PBSSales *pbsData = [[PBSSales alloc] init];

    if(jsonResponseAsDictionary != nil)
    {
        // Process the hourly sales figures if the day request and returned is related to Daily figures
        if([calendarViewType isEqualToString:@"Day"]){
            NSArray *hourlyFiguresFromJson = [jsonResponseAsDictionary objectForKey:@"hourlySalesFigures"];

            PBSDataDaySales *tmpDataDay = [[PBSDataDaySales alloc] init];
            NSMutableArray *hSalesFigures = [tmpDataDay hourlySalesFigures];

            for(NSInteger i = 0; i < [hourlyFiguresFromJson count]; i++){
                hSalesFigures[i] = hourlyFiguresFromJson[i];
            }

            [[pbsData dataDay] setHourlySalesFigures:hSalesFigures];
            [pbsData setCalViewType:@"Day"];
        }

    }

    return pbsData;
}
John Cogan
  • 1,034
  • 4
  • 16
  • 39