2015-02-12 2 views
-5

Я пытаюсь практиковать синтаксический анализ данных jSON, используя самый популярный канал youTube, и использовать значения для подключения к таблице.Анализ данных JSON с объективом C

Вот мой код, чтобы получить:

NSURL *url = [NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setTimeoutInterval:15.0]; 

_jsonArray = [[NSMutableArray alloc]init]; 

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 

    if (connectionError == NULL) 
    { 
     NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&connectionError]; 

     NSDictionary *firstlayer = [responseDictionary objectForKey:@"feed"]; 

     NSDictionary *secondLayer = [firstlayer objectForKey:@"entry"]; 

     _jsonArray = [secondLayer valueForKey:@"title"]; 

     NSLog(@"%@",_jsonArray); 


     [self.tableView reloadData]; 

    }else{ 
     NSLog(@"Connection failed, please try again later"); 
    } 
}]; 

Вот ответ:

2015-02-12 11:37:44.679 RestDemo[27043:2384786] (
     { 
     "$t" = "Steven Yeun & Conan Visit A Korean Spa - CONAN on TBS"; 
    }, 
     { 
     "$t" = "Woman in minivan stops high speed chase in Dallas - 2/11/15"; 
    }, 
     { 
     "$t" = "Anything Can Be Sexy with Dakota Johnson"; 
    }, 
     { 
     "$t" = "Pitch Perfect 2 \U2013 Official Trailer 2 (HD)"; 
    }, 
     { 
     "$t" = "Bill Belichick Talks About Deflategate with David Letterman"; 
    }, 
     { 
     "$t" = "\"Trainwreck\" Official Red Band Trailer"; 
    }, 
     { 
     "$t" = "TRAINWRECK Trailer #1 (2015) Judd Apatow Comedy Movie HD"; 
    }, 
     { 
     "$t" = "HEAT MAP Microwave- a NEW invention"; 
    }, 
     { 
     "$t" = "The Transgender Fight For Safe Bathrooms"; 
    }, 
     { 
     "$t" = "This Is SportsCenter - Sharks"; 
    }, 
     { 
     "$t" = "23 Weird Awards - mental_floss on YouTube - List Show (246)"; 
    }, 
     { 
     "$t" = "Honest Trailers - Boyhood"; 
    }, 
     { 
     "$t" = "Dr. Phil on Punishment Haircuts"; 
    }, 
     { 
     "$t" = "Calvin Harris - Pray to God ft. HAIM"; 
    }, 
     { 
     "$t" = "Understanding Boko Haram"; 
    }, 
     { 
     "$t" = "Guys Read Each Other\U2019s Texts To Their Girlfriends | Elite Daily"; 
    }, 
     { 
     "$t" = "Here\U2019s the first behind the scenes footage of SPECTRE."; 
    }, 
     { 
     "$t" = "STAR WARS - CROSSGUARD LIGHTSABER"; 
    }, 
     { 
     "$t" = "Florence + The Machine - How Big How Blue How Beautiful"; 
    }, 
     { 
     "$t" = "DSCOVR Launches Aboard SpaceX Falcon 9"; 
    }, 
     { 
     "$t" = "Sergei Polunin, \"Take Me to Church\" by Hozier, Directed by David LaChapelle"; 
    }, 
     { 
     "$t" = "What Are Measles Parties?"; 
    }, 
     { 
     "$t" = "Sevyn Streeter - Don't Kill The Fun ft. Chris Brown [Official Video]"; 
    }, 
     { 
     "$t" = "Sia - Chandelier (57th GRAMMYs feat. Kristen Wiig & Maddie Ziegler)"; 
    }, 
     { 
     "$t" = "50 Shades of Buscemi (Trailer Recut)"; 
    } 
) 

Как разворачивать его из этого массива, чтобы назначить их в Tableview.

+0

Вы можете «назначить», что массив в Tableview прямо сейчас - нет никаких ограничений на то, что вы можете использовать для DataSource. –

ответ

0

Ups, я буду редактировать:

Чтобы показать массив JSON в виде таблицы, вы должны использовать UITableView делегат и источник данных.

self.tableView.delegate = self; 
self.tableView.dataSource = self; 

А затем реализовать источники и делегатов данных Tableview:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _jsonArray.count; 
} 
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"] autorelease]; 
    cell.textLabel.text = [_jsonArray objectAtIndex:row]; 
    return cell; 
} 
+0

Снова прочитайте вопрос. Результат, указанный в вопросе, - '_jsonArray'. Кроме того, речь идет не о разборе JSON. Вопрос о том, как получить полученный массив данных в виде таблицы, является чрезмерно широким. – rmaddy

Смежные вопросы