1

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.

Community
  • 1
  • 1

4 Answers4

3
-(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"]);
   }
}
hhanesand
  • 990
  • 11
  • 28
jailani
  • 2,260
  • 2
  • 21
  • 45
  • Thanks, I would then call this from my ```initWithDict``` method? Or would I create a different class. The info would be displayed on the same view. I display my others by doing ```self.tideModel.XXX``` and update my label text. –  Dec 30 '13 at 21:06
  • It depends on your need typically different class is better approach to handle the parsed json. Then you can access easily otherwise have to use valueForKey. – jailani Dec 31 '13 at 05:09
  • Thanks again for this, I have it displaying. I want to display several parts of this. How would I bind this to a model so I can do: ```tide.data.type``` or ```tide.date.pretty``` - I looked in to JSONModel, but that takes arrays, I have nested objects. Thoughts? Or a pointer in the right direction? –  Jan 01 '14 at 17:59
0

You can take a look at Initialize NSArray with data from NSdictionary as I have already answered this Hope this helps.

Community
  • 1
  • 1
Vish
  • 341
  • 1
  • 4
  • 19
0

If I'm understanding what you are trying to do, you should be able to replace this part:

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

with something like:

for (NSDictionary *tideSummary in arrOfTideSummary) {
    NSDictionary *date = [tideSummary valueForKey:@"date"];
    NSLog(@"Pretty date: %@", [date valueForKey:@"pretty"]);
}

If you want the date values in a table of some sort, then you could store them into a new array or some other data structure and then create a table view that uses that as its data source.

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
0

I have below data on JSON link...

{
"results":[
    {
        "address_components": [
            {
                "long_name": "Satadhar",
                "short_name": "Satadhar",
                "types":[
                    "point_of_interest",
                    "establishment"
                ]
            } ]
]

I fetched data through ASIHTTPREQUEST so the code is below given

-(void)requestFinished:(ASIHTTPRequest *)request {
        NSError *err;
        NSData *data=[request responseData];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data   options:kNilOptions error:&err];

        arr=[[dict valueForKey:@"results"]valueForKey:@"address_components"]; //to fetch the data till inside Starting point i.e address_component

        _arrTable=[[NSMutableArray alloc]init]; //take a array to store the data of the index 0 [index 0 store the whole data that is start from [array] ]

        _arrTable =[arr objectAtIndex:0]; //get the data of 0th index

        NSLog(@" whole data%@",_arrTable);//print it u will get the idea

        NSLog(@" my %@",[[arr objectAtIndex:0] valueForKey:@"long_name"]);
        NSLog(@" my %@",[[arr objectAtIndex:0] valueForKey:@"short_name"]);

        [self.tblview reloadData];
    }
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81