Convert JSON data into a model
Convert JSON data into a model
// 1. Create a url
NSURL * url = kSUNUrl (@ "video ");
// 2. Create a request
NSURLRequest * request = [NSURLRequestrequestWithURL: url];
// 3. Send request data
NSOperationQueue * queue = [NSOperationQueuemainQueue];
[NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError ){
If (connectionError | data = nil ){
[MBProgressHUD showError: @ "Network timeout. Please wait..."];
Return;
}
// 4. parse json data
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error: nil];
NSArray * videosArray = dict [@ "videos"];
For (NSDictionary * dictin videosArray ){
[_ ArrayM addObject: [SUNVideoItem videoWithDict: dict];
}
// 5. Refresh the table
[Self. tableView reloadData];
}];
Note: after sending network data, you must refresh the table.