如何从url ini iOS无响应输出数据中解析json数据

时间:2014-03-05 05:36:26

标签: ios objective-c json parsing

我在iOS中解析json数组时遇到问题,错误在于Objective-C源代码中json的结构,我附上了objective-c和JSON,这是我的源代码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];
}
}

这是我的源代码ViewController.h:

#import <UIKit/UIKit.h>

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

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

这是我的JSON数据:

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

抱歉这一切都行不通。 这是我的更新代码

- (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;
}

但它仍然没有输出响应。你能告诉我那个代码有什么问题吗? 对不起我在iOS编程人员的新手..

2 个答案:

答案 0 :(得分:0)

这是代码

- (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];

}

解析数据后,您将直接获取数据..

答案 1 :(得分:0)

你得到了回复,现在你想解析数据。

你已经完美地完成了网络服务,现在你需要做的就是将它解析成一个数组 首先下载此链接中的SBJSON文件

  

https://github.com/stig/json-framework/

然后,将它们复制到您的工作区。然后,在viewController中添加此

#import "SBJson.h"  

您的JSON数据包含字典

形式的值

所以,解析他们

 SBJsonParser * parser=[SBJsonParser new];
 NSDictionary * jsonData=(NSDictionary *)[parser objectWithString:outputData];
 NSDictionary * dict=(NSDictionary *)[NSDictionary objectForKey:@"genres"];
 NsDictionary * id=(NSDictionary*)[dict objectForKey:@"id"];
 NsDictionary * name=(NSDictionary*)[dict objectForKey:@"name"];

我认为这会有所帮助