How to analyze json data from in-iOS output data in-iO-in-Response

advertisements

I have problems in parsing json array in iOS, the error lies in the structure of the json in Objective-C source code, I attach objective-c and JSON, this is my source code ViewController.m :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData   options:0 error:nil];
NSDictionary *JSON = [allDataDictionary objectForKey:@"JSON"];
NSArray *arrayOfJSON = [JSON objectForKey:@"genres"];

for (NSDictionary *diction in arrayOfJSON) {
    NSDictionary *JSON = [diction objectForKey:@"genres"];
    NSString *id = [feed objectForKey:@"id"];
    NSString *name = [feed objectForKey:@"name"];

    [array addObject:id];
    [array addObject:name];

}
[[self myTableView ]reloadData];

}
- (IBAction)getData:(id)sender {

NSURL *url = [NSURL URLWithString:@"http://api.themoviedb.org/3/genre/list?api_key=65b56d5d3fa43ad24d10b2786b3d0b96"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

Connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(Connection)
{
    webData = [[NSMutableData alloc]init];
}
}

this is my source code ViewController.h :

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,    UITableViewDelegate, NSURLConnectionDataDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

- (IBAction)getData:(id)sender;
@end

this is my JSON data :

{"genres":[{"id":28,"name":"Action"},{"id":12,"name":"Adventure"},     {"id":16,"name":"Animation"},{"id":35,"name":"Comedy"},{"id":80,"name":"Crime"},{"id":105,"name":"Disaster"},{"id":99,"name":"Documentary"},{"id":18,"name":"Drama"},{"id":82,"name":"Eastern"},{"id":2916,"name":"Erotic"},{"id":10751,"name":"Family"},{"id":10750,"name":"Fan Film"},{"id":14,"name":"Fantasy"},{"id":10753,"name":"Film Noir"},{"id":10769,"name":"Foreign"},{"id":36,"name":"History"},{"id":10595,"name":"Holiday"},{"id":27,"name":"Horror"},{"id":10756,"name":"Indie"},{"id":10402,"name":"Music"},{"id":22,"name":"Musical"},{"id":9648,"name":"Mystery"},{"id":10754,"name":"Neo-noir"},{"id":1115,"name":"Road Movie"},{"id":10749,"name":"Romance"},{"id":878,"name":"Science Fiction"},{"id":10755,"name":"Short"},{"id":9805,"name":"Sport"},{"id":10758,"name":"Sporting Event"},{"id":10757,"name":"Sports Film"},{"id":10748,"name":"Suspense"},{"id":10770,"name":"TV movie"},{"id":53,"name":"Thriller"},{"id":10752,"name":"War"},{"id":37,"name":"Western"}]}


Sorry it all not working. Here my update code

- (NSArray *) genreList
{
     if(!gendreList)
{

    NSURL *url = [NSURL URLWithString:@"http://api.themoviedb.org/3/genre/list?api_key=65b56d5d3fa43ad24d10b2786b3d0b96"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response,
                                               NSData *data, NSError *connectionError)
     {
         if (data.length > 0 && connectionError == nil)
         {
             NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

             gendreList = [[jsonDict valueForKey:@"genres"] valueForKey:@"name"];
            self.idGenreList = [[jsonDict valueForKey:@"genres"] valueForKey:@"id"];

                          }
         else if (connectionError != nil)
         {
             NSLog(@"No Internet!");
         }
     }];
}
   self.gendreList = gendreList;
 NSLog(@"%@",self.genreList);
return gendreList;
}

But it still no output response. Can you tell me what is a wrong on thats code?? Sorry i Newbie on iOS programming guys ..


Here is the code

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData   options:0 error:nil];
     NSArray *arrayOfJSON = [allDataDictionary objectForKey:@"genres"];

     for (NSDictionary *diction in arrayOfJSON) {
             NSString *id = [diction objectForKey:@"id"];
             NSString *name = [diction objectForKey:@"name"];

             [array addObject:id];
             [array addObject:name];

     }
     [[self myTableView ]reloadData];

}

After you parse data, you will get data directly..