Thursday, 25 June 2015

JSON Parser


#import "ViewController.h"

@interface ViewController ()
{
    NSMutableDictionary *arraykey;
    NSMutableArray *arraydata;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://www.json-generator.com/api/json/get/bUyjbCQmxu?indent=2"]];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
    {
        if (!connectionError) {
            arraykey = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            arraydata = [[NSMutableArray alloc]init];
            [arraydata addObjectsFromArray:[[[arraykey objectForKey:@"items"]objectAtIndex:0] allKeys]];
            NSLog(@"%@",arraydata);
            NSLog(@"%@",arraykey);
            [tbljson reloadData];
        } else {
            NSLog(@"error: %@",[connectionError localizedDescription]);
        }
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arraydata.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusecell"];
   
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reusecell"];
    }
   
   cell .textLabel.text = [arraydata objectAtIndex:indexPath.row];
   
    return cell;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

No comments:

Post a Comment