I am working on my first iOS app and I'm also new to using an API. I'm trying to figure out how to access data through the API.
(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // create the URL we'd like to query NSURL myURL = [[NSURL alloc]initWithString:@"http://api.themoviedb.org/3/movie/550?api_key="];
// we'll receive raw data so we'll create an NSData Object with it
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
// now we'll parse our data using NSJSONSerialization
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
// typecast an array and list its contents
NSArray *jsonArray = (NSArray *)myJSON;
// take a look at all elements in the array
for (id element in jsonArray) {
NSLog(@"Element: %@", [element description]);
}
}
That's the code I am currently using (with an API key filled in of course). When I run this code I get things like "Element: release_date". How do I access the actual release date, the data associated with release_date?
찾으시는 영화나 TV 프로그램이 없나요? 로그인 하셔서 직접 만들어주세요.
이 항목을 평가하거나 목록에 추가할까요?
회원이 아닌가요?
littlereddoor님의 댓글
8월 9, 2013 at 7:39 오전
Hi nevrothwen,
I haven't taken a look at the data returned by the API yet, so I can be specific until I have done so. However, you want to be looking at using the valueForKey: method on the array.
NSString *releaseDate = [jsonArray valueForKey:@"releasedate"];
That should get you the release date?
nevrothwen님의 댓글
8월 11, 2013 at 6:36 오전
Thanks. I managed to figure it out after a while. Still trying to get my head around JSON and such :-)