◎위챗 : speedseoul
얻어지는 Json 데이터값이 배열로 들어오는지 딕셔너리로 들어오는지 판단 필요함
Answered by Vin is right. Basically, to parse json response look at the kind of bracket used.
Start parsing with the outer symbol and check for the symbol. If it is,
1) {
then it is NSDictionary. <----- objectForKey 사용
2) [
then it is NSArray. <------ valueForKey 사용
These simple rules will make your life easy. :)
{
"Albumvideo":[
{
"titre": "Publicité",
"photo":"blabla.jpg"
},
{
"titre": "Events",
"photo":"blabla.jpg"
}
]
}
- (void) viewDidLoad { [super viewDidLoad]; dispatch_async (kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL:lienAlbumsVideo]; [self performSelectorOnMainThread:@selector(fetchedData:)withObject:data waitUntilDone:YES]; }); } - (void)fetchedData:(NSData *)responseData {
NSError* error;NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];NSArray* albumsvideo = [json objectForKey:@"Albumvideo"]; NSString *titre1 = [[albumsvideo objectAtIndex:0]valueForKey:@"titre"]; NSString *titre2 = [[albumsvideo objectAtIndex:1]valueForKey:@"titre"];