Domanda

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?

È stato utile?

Soluzione

  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:

Altri suggerimenti

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top