MIrfan MIrfan - 14 days ago 5x
iOS Question

Count number of values in dictionary object

products = (
id = 19;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330374078photography.jpg";
name = "Save $240 on your next photo session";
id = 21;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330373696massage.jpg";
name = "One Hour Massage";

the above is what I got through json, I need to assign the values to uitableviews:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"Number of rows: %d",[[products objectForKey:@"products"] count]);
return [[products objectForKey:@"products"] count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]
NSString *currentProductName;
currentProductName = [productKeys objectAtIndex:indexPath.row];
NSLog(@"product name : %@", currentProductName);
[[cell textLabel] setText:currentProductName];

return cell;

it returns 0 number of rows, I am newbie to ios please help me how I will assign these values to uitableview.



The problem is that what you posted is not json. It should be

            "name":"Save $240 on your next photo session"
            "name":"One Hour  Massage"

You can use http://jsonlint.com/ to validate your json files.

I have used the above json file and did the following:

NSBundle* bundle = [NSBundle mainBundle];
NSString *jsonPath = [bundle pathForResource:@"data" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];

NSError *error;
NSDictionary *products = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

DLog(@"products: %i", [[products objectForKey:@"products"] count] );

[self.tableView reloadData];

Result is: products: 2.

You should be able to reproduce this.