Coughlin Coughlin - 12 days ago 6
iOS Question

Accessing an array with nested objects and displaying keys values

I have this data set here: https://gist.github.com/ryancoughlin/8043604 - If you see

tide.tideSummary
it contains an array but inside, it contains multiple dictionaries. I am trying access and display
tide.tideSummary.date.pretty
and display (all 33 of them) in some type of table (but that I have working with dummy data).

I was going through a somewhat similar question here: Keypath for first element in embedded NSArray

Having some trouble finding a solution to access those nested values. Been through plenty of tutorials and posts here on StackOverflow that deal with very basic JSON strings and those work great for me.




UPDATE:

Came across this question: Parsing nested JSON objects with JSON Framework for Objective-C

In my case, I have the following:

NSArray *prettyDate = [[tide objectForKey:@"tideSummary"] valueForKey:@"date"];


prettyDate
prints this from
NSLog


Pretty date array: (
{
epoch = 1388388109;
hour = 02;
mday = 30;
min = 21;
mon = 12;
pretty = "2:21 AM EST on December 30, 2013";
tzname = "America/New_York";
year = 2013;
},
{
epoch = 1388397506;
hour = 04;
mday = 30;
min = 58;
mon = 12;
pretty = "4:58 AM EST on December 30, 2013";
tzname = "America/New_York";
year = 2013;
},
{
epoch = 1388405656;
hour = 07;
mday = 30;
min = 14;
mon = 12;
pretty = "7:14 AM EST on December 30, 2013";
tzname = "America/New_York";
year = 2013;
}


Then I would be able to loop through each, grab the object and display?




Would I do something like:


  • Grab parent item
    tideSummary
    - array

  • Grab the item above and store
    date
    - dictionary

  • Access
    pretty
    via
    objectForKey



I have this
initWithDict


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

if(self) {

NSDictionary *tide = [json valueForKeyPath:@"tide"];

NSArray *arrOfTideSummaryStats = [json valueForKeyPath:@"tide.tideSummaryStats"];

NSDictionary *dctOfTideSummaryStats = [arrOfTideSummaryStats objectAtIndex:0];

NSArray *arrOfTideSummary = [json valueForKeyPath:@"tide.tideSummary"];

// Loop through the date then...
// Loop and grab 'pretty'

// The "first" is the from the example link in my question above. Experimental oonly
id pretty = [arrOfTideSummary valueForKeyPath: @"@first.tideSummary.date.pretty"];

// This all works below

self.maxheight = [NSString stringWithFormat:@"%@", [dctOfTideSummaryStats valueForKey: @"maxheight"]];
self.minheight = [NSString stringWithFormat:@"%@", [dctOfTideSummaryStats valueForKey: @"minheight"]];

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

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

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

return self;
}


Does anyone have any examples or a direction to steer me in? Would love a resource or question to work off of.

Answer
-(void)parseJson:(id)json{
   NSDictionary *tide = [json objectForKey:@"tide"];
   NSArray *tideSummary = [tide objectForKey:@"tideSummary"];

   for (int i = 0; i < tideSummary.count; i++) {  
      NSDictionary *eachTideSummary = [tideSummary objectAtIndex:i];

      NSDictionary *dateDic = [eachTideSummary objectForKey:@"date"];
      NSDictionary *utcdateDic = [eachTideSummary objectForKey:@"utcdate"];

      NSLog(@"Preety from date dictionary: %@", [dateDic objectForKey:@"pretty"]);
      NSLog(@"Preety from utcdate dictionary: %@", [utcdateDic objectForKey:@"pretty"]);
   }
}
Comments