Im newly using AFNetworking
to get JSON
and parse it,I've imported it to my project and got the JSON
but i don't know how is it possible to parse the JSON
for display especially in Objective-c.
Here is the code in my viewDidLoad
to Get JSON
:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://api.androidhive.info/json/movies.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
The Retrieved JSON
for parsing:
{
genre = (
Action,
Drama,
"Sci-Fi"
);
image = "http://api.androidhive.info/json/movies/1.jpg";
rating = "8.300000000000001";
releaseYear = 2014;
title = "Dawn of the Planet of the Apes";
},
{
genre = (
Action,
"Sci-Fi",
Thriller
);
image = "http://api.androidhive.info/json/movies/4.jpg";
rating = "8.4";
releaseYear = 2014;
title = "X-Men: Days of Future Past";
},
{
genre = (
Action,
Adventure,
Fantasy
);
image = "http://api.androidhive.info/json/movies/7.jpg";
rating = "7.3";
releaseYear = 2014;
title = "The Amazing Spider-Man 2";
},
{
genre = (
Animation,
Comedy,
Family
);
image = "http://api.androidhive.info/json/movies/9.jpg";
rating = "8.300000000000001";
releaseYear = 2013;
title = Rush;
},
{
genre = (
Animation,
Adventure,
Family
);
image = "http://api.androidhive.info/json/movies/15.jpg";
rating = "8.199999999999999";
releaseYear = 2010;
title = "How to Train Your Dragon";
}
)
Update :
How is it possible to display it and store the retrieved data so its easier to show it in UITableView
.
i tried to do like :
NSLog(@"JSON: %@", [responseObject objectForKey:@"image"]);
to get the whole images only but it crashes. i just need to get for example all the titles and store them in array and then display them UITableView
.
you try this way to get data from NSDictionary
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://api.androidhive.info/json/movies.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
self.arrData=[[NSMutableArray alloc]initWithArray:responseObject];
[self.tableView reloadData];// reload table data
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
TableView Delegate Method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// your cell code here
// indexPath.row(use in tableView) means your number of index to get value
NSLog(@"JSON: %@", [[self.arrData objectAtIndex:indexPath.row] objectForKey:@"image"]);
NSLog(@"title: %@", [[self.arrData objectAtIndex:indexPath.row] objectForKey:@"title"]);
NSLog(@"rating: %@", [[self.arrData objectAtIndex:indexPath.row] objectForKey:@"rating"]);
NSLog(@"releaseYear: %@", [[self.arrData objectAtIndex:indexPath.row] objectForKey:@"releaseYear"]);
NSLog(@"genre: %@", [[self.arrData objectAtIndex:indexPath.row] objectForKey:@"genre"]);// return array get value particular using array index
}