0

I'm attempting to get a json file from a website into UITableview with Objective C. As I'm not an advanced coder please excuse my crude coding tecniques. I have a .json file uploaded to my webspace. The File is formatted as so:

"JSONDATA":[
       {
           "name": "ABC",
           "details": "DEF"
       },
       {
           "name": "UVW",
           "details": "XYZ"
       }
] 

my .h looks like:

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

{
    IBOutlet UITableView *myTableView;
}
@property (strong, nonatomic) IBOutlet UITableView *myTableView;


@end

the .m code

#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

I can't seem to put all the pieces together and get any data through to parse. The NSLog is telling me:

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 }

Where is this thing derailing? I can't get any data out of the URLResponse. Thanks so much.

1 Answer 1

1

You have to resume the data task and you have to reload the table view inside the completion block on the main thread

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
    ...
  }
  dispatch_async(dispatch_get_main_queue(), ^{
    [myTableView reloadData];
  });
}] resume];
1
  • Thanks!! That worked after I used a json validator and discovered my json was bad. I was starting the file with: "JSONDATA":[ when I should have used: {"JSONDATA":[ and also end the file with a: ]}
    – Mick
    Commented Mar 14, 2020 at 13:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.