sixstatesaway
sixstatesaway

Reputation: 1106

Populate UITableView with nested JSON

as the title suggest I'm trying to populate a UITableView from JSON. I get the json object which looks like

  {
    applications: {
           iphone: {
             application: [
                            {
                              title: "Title",
                             },
                             {
                              title: "Title2",
                             },
                             {
                              title: "Title3",
                             }
                           ]
                      }
                     }
    }

it goes on like that, with several title fields nested inside of application.

I would like to use all the titles as my cell labels. I can get a list like so

for (int i = 0; i < 10; i++)
    {
        titleString = [[[[[result objectForKey:@"applications"] objectForKey:@"iphone"] objectForKey:@"application"] objectAtIndex:i] objectForKey:@"title"];
        NSLog(@"title %@", titleString);
    }

which does work, as in I can log the list of titles...but I know I'm going about this in the wrong way, I can't even figure out what myArray would be in the return [myArray count] in the numberOfRowsInSection method... totally lost here! help is appreciated!

Upvotes: 0

Views: 585

Answers (3)

Abhishek
Abhishek

Reputation: 2253

First is Application dictionary
this Application dictionary has one dictionary of key iPhone
this iphoneDic has an array of Dictionary

NSMutableDictionary *applicationsDict = [response objectForKey:@"applications"];
NSMutableDictionary *iphoneDict = [applicationsDict objectForKey:@"iphone"];
NSMutableArray *applicationArray = [iphoneDict objectForKey:@"application"];

now in the method numberOfRowInSection: of table view return the array count

return [applicationArray count]

now in the cellForRowAtIndexPath: mthod we will use this applicationArray
Use Array ...

NSMutableDictionary *titleDict = [applicationArray objectAtIndex:indexPath.row];
cell.titleLabel.text = [titleDict objectForKey:@"title"];

I hope this will help you

Upvotes: 0

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

[[[result objectForKey:@"applications"] objectForKey:@"iphone"] objectForKey:@"application"] is going to return array, so better store this array and use it wherever is required as follows:

NSArray *titleArray = [[[result objectForKey:@"applications"] objectForKey:@"iphone"] objectForKey:@"application"];

To store this array you can use instance variable so that you can use it entire file.

Upvotes: 1

Anila
Anila

Reputation: 1136

You can store these titles in an array then use this array as datasource for the table. Your myArray would be the array your stored titles in.

Upvotes: 0

Related Questions