user3273254
user3273254

Reputation: 85

load local json information in UITableView

I have my own json file "list.json" for a list of sample informations like below. My json file is located inside of Xcode, I want to show my information to the table, would you please give me some hints and help for this implementation, how can I parse local json and load information in table.

[
{
    "number": "1",
    "name": "jon"
},
{
    "number": "2",
    "name": "Anton"
},
{
    "number": "9",
    "name": "Lili"
},
{
    "number": "7",
    "name": "Kyle"
},
{
    "display_number": "8",
    "name": "Linda"
}
]

Upvotes: 5

Views: 7397

Answers (1)

Toan Nguyen
Toan Nguyen

Reputation: 11591

you can create a custom class which inherits from UITableViewController.

The code to read the contents of list.json file into an array is:

NSString * filePath =[[NSBundle mainBundle] pathForResource:@"list" ofType:@"json"];

    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }


    self.dataList = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL];

And the header file is:

#import <UIKit/UIKit.h>

@interface TVNA_ReadingDataTVCViewController : UITableViewController

@end

The implementation is:

#import "TVNA_ReadingDataTVCViewController.h"

@interface TVNA_ReadingDataTVCViewController ()

@property  NSArray* dataList;

@end

@implementation TVNA_ReadingDataTVCViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    [self readDataFromFile];

    [self.tableView reloadData];
}


-(void)readDataFromFile
{
    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"list" ofType:@"json"];

    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }


    self.dataList = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    id keyValuePair =self.dataList[indexPath.row];

    cell.textLabel.text = keyValuePair[@"name"];

    cell.detailTextLabel.text=[NSString stringWithFormat:@"ID: %@", keyValuePair[@"number"]];
    return cell;
}


@end

Finally, on your story board, assign this class as the custom class of your Table View Controller. Hope this helps.

Upvotes: 12

Related Questions