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?

1
  • tide.tideSummaryStats returns an array. infact even tide.tideInfo returns an array. also, it shouldn't be [dictionary valueForKeyPath:...] it should be [json valueForKeyPath:...] Commented Dec 28, 2013 at 19:32

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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 (it's quite old, 2011 and i am not sure if things are any different now)
@Coughlin : added one more link in the answer that may be of some help to you. check: link
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?
I tried it do display ALL my data, but I think it was overkill since it looks primarily for arrays.
|
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.