解析 JSON 从 AFNetworking 获取到显示在 UITableView
Parse JSON get from AFNetworking to display in UITableView
我最近使用 AFNetworking
来获取 JSON
并解析它,我已经将它导入我的项目并获得了 JSON
但我不知道怎么可能解析 JSON
以便显示,尤其是在 Objective-c 中。
这是我 viewDidLoad
中用于获取 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);
}];
检索到 JSON
用于解析:
{
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";
}
)
更新:
如何显示它并存储检索到的数据以便更容易在 UITableView
中显示它。
我试着这样做:
NSLog(@"JSON: %@", [responseObject objectForKey:@"image"]);
只获取整个图像但它崩溃了。例如,我只需要获取所有标题并将它们存储在数组中,然后显示它们 UITableView
.
你试试这个方法从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 委托方法
-(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
}
AFNetworking returns 已经在 responseObject
中处理了 JSON
- 在您的情况下,它是其中包含 NSDictionary 对象的 NSArray。
要访问单个 NSDictionary,请创建一个 NSArray 属性(例如 resultArray
)并将其设置为 responseObject
,然后您可以通过 resultArray[1]
(使一定要退回支票)。
然后您可以通过键(myDictionary[@"title]
等)访问 dictionary
的值。
我最近使用 AFNetworking
来获取 JSON
并解析它,我已经将它导入我的项目并获得了 JSON
但我不知道怎么可能解析 JSON
以便显示,尤其是在 Objective-c 中。
这是我 viewDidLoad
中用于获取 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);
}];
检索到 JSON
用于解析:
{
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";
}
)
更新:
如何显示它并存储检索到的数据以便更容易在 UITableView
中显示它。
我试着这样做:
NSLog(@"JSON: %@", [responseObject objectForKey:@"image"]);
只获取整个图像但它崩溃了。例如,我只需要获取所有标题并将它们存储在数组中,然后显示它们 UITableView
.
你试试这个方法从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 委托方法
-(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
}
AFNetworking returns 已经在 responseObject
中处理了 JSON
- 在您的情况下,它是其中包含 NSDictionary 对象的 NSArray。
要访问单个 NSDictionary,请创建一个 NSArray 属性(例如 resultArray
)并将其设置为 responseObject
,然后您可以通过 resultArray[1]
(使一定要退回支票)。
然后您可以通过键(myDictionary[@"title]
等)访问 dictionary
的值。