我在解析iOS中的json數組時遇到問題,錯誤在於Objective-C源代碼中json的結構,我附加了objective-c和JSON ,這是我的源代碼ViewController.m:如何從url ini解析json數據iOS無響應輸出數據
- (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編程的傢伙..
那麼什麼是錯誤? – Zen
'NSDictionary * JSON = [allDataDictionary objectForKey:@「JSON」]的用途是什麼;'有沒有與我們分享的JSON級別? –
NSDictionary * JSON = [allDataDictionary objectForKey:@「JSON」];不需要...你可以直接調用這個方法。NSArray * arrayOfJSON = [allDataDictionary objectForKey:@「genres」]; –