Я пытаюсь получить файл json с веб-сайта в UITableview с Objective C. Поскольку я не продвинутый программист, прошу прощения за мои грубые методы кодирования. У меня есть файл. json, загруженный в мое веб-пространство. Файл отформатирован так:
"JSONDATA":[
{
"name": "ABC",
"details": "DEF"
},
{
"name": "UVW",
"details": "XYZ"
}
]
my .h выглядит так:
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UITableView *myTableView;
}
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@end
.m код
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arrName;
NSMutableArray *arrDetails;
NSString *responseString;
}
@end
@implementation ViewController
@synthesize myTableView;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"ViewDidLoad");
[self fetchData];
[myTableView reloadData];
}
-(void)fetchData
{
NSLog(@"GetJsonResponse Fired");
NSURL *URL = [NSURL URLWithString:@"http://myWebsite.com/json/myJsonFile.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSLog(@"URLRequest = %@",request);
/////////////////////////////////////////////////////////////////// Nothing Below Here Fires/////////////////////////////////////////////////
// [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock];
[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
{
// Block Body
NSLog(@"response = %@",response);
NSLog(@"block Body");
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"GetJsonDict%@",jsonDict);
NSArray *arr = jsonDict[@"JSONFILE"];
NSLog(@"jasoDict = %@",arr);
self->arrName = [[NSMutableArray alloc]init];
self->arrDetails = [[NSMutableArray alloc]init];
//arrValue = [[NSMutableArray alloc]init];
for(int i=0;i<[arr count];i++)
{
NSString *strName = [arr[i]objectForKey:@"NAME"];
NSString *strCode = [arr[i]objectForKey:@"CODE"];
// NSString *strValue = [arr[i]objectForKey:@"VALUE"];
NSLog(@"The strName is - %@",strName);
NSLog(@"The strCode is - %@", strCode);
// NSLog(@"The strValue is - %@", strValue);
[self->arrName addObject:strName];
[self->arrDetails addObject:strCode];
// [arrValue addObject:strValue];
NSLog(@"The arrName is - %@",self->arrName);
NSLog(@"The arrDetails is - %@", self->arrDetails);
// NSLog(@"The arrValue is - %@", arrValue);
}
}];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrName.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = @"Cell";
// UITableViewCell *cell = [UITableView dequeueReusableCellWithIdentifier:strCell];
UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:strCell forIndexPath:indexPath];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCell];
}
cell.textLabel.text = arrName[indexPath.row];
cell.detailTextLabel.text = arrDetails[indexPath.row];
return cell;
}
@end
Я не могу собрать все части вместе и получить любые данные для анализа. NSLog говорит мне:
2020-03-09 14:13:42.605558-0500 jsonFromWeb[27389:1317924] ViewDidLoad
2020-03-09 14:13:42.605802-0500 jsonFromWeb[27389:1317924] GetJsonResponse Fired
2020-03-09 14:13:42.606118-0500 jsonFromWeb[27389:1317924] URLRequest = <NSURLRequest: 0x6000001f8cc0> { URL: http://mywebsite.com/json/myJsonFile.json }
Где это крушение? Я не могу получить какие-либо данные из URLResponse. Большое спасибо.