4
votes
<pre>
    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";
        }
    );
}
</pre>

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]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"cell"];
        }
        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.

Regards

1
I need more information. This code looks okay. It goes wrong in the data retrieval part of this story. Can you add the code of how you fill the products dictionary?Tom van Zummeren
can u log the products and show us..? if the products itself is the array you can just its count right?vishy
products = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; this is how the products is populated,, and when i log the contents with in first pre tag in question is printedMIrfan
check in numberOfRowsInSection, whether the products is having values or nilarthankamal
numberOfRowsInSection return nilMIrfan

1 Answers

5
votes

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

{
    "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"
        }
    ]
}

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.