JSON数据转化成模型
// 1.创建url
NSURL
*url =
kSUNUrl(@"video");// 2.创建request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.发送请求数据
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError || data == nil) {
[MBProgressHUD showError:@"网络超时,请稍后..."];
return ;
}
// 4.解析json数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *videosArray = dict[@"videos"];
for (NSDictionary *dict in videosArray) {
[_arrayM addObject:[SUNVideoItem videoWithDict:dict]];
}
// 5.刷新表格
[self.tableView reloadData];
}];
注意:
在发送网络数据结束之后,一定要刷新表格。
该博客介绍了如何将JSON数据转换为模型对象。首先创建URL,然后构建NSURLRequest,接着发送异步请求获取数据。在接收到数据后,通过NSJSONSerialization解析JSON,将结果转化为字典,并进一步转换为自定义模型SUNVideoItem,最后刷新UITableView展示数据。
10万+

被折叠的 条评论
为什么被折叠?



