4
<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

6
  • 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? Commented Dec 13, 2012 at 9:17
  • can u log the products and show us..? if the products itself is the array you can just its count right? Commented Dec 13, 2012 at 9:19
  • 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 printed Commented Dec 13, 2012 at 9:20
  • check in numberOfRowsInSection, whether the products is having values or nil Commented Dec 13, 2012 at 9:24
  • numberOfRowsInSection return nil Commented Dec 13, 2012 at 9:26

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

the actual json i produced is the same as you mentioned, i print here what ever is logged by ios. the json is valid i have checked with your provided url.
Yes please i need more help because my json format is correct.
I have point out that products with my original code is well populated in viewDidLoad method but when i call numberOfRowsInSection it return me 0, why is it behaving like this.
Have you added [self.tableView reloadData]; after the data is loaded?
on which method should i add this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.