2

-- UPDATED QUESTION WITH THE MAIN JSON FILE --

I'm trying to add names to a table view. But the table is always empty. (Xcode 7.1b1, iOS 9.1b2) This is just a sample .json file I'm testing my code with. The main json file has a lot more data.

I was successful in adding the data from the sample json file. But now after using the main json file, the table is empty again.

.json file

[
  {
    "150978": {
      "jobs": {
        "D0-C14008-CW": {
          "05-9032": {
            "G-05": {
              "hours_rwk": "False",
              "hours_st": "4L",
              "hours_ewo_id": "D0-C14008-CWddda",
              "hours_ewo": "True",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          },
          "05-9031": {
            "B-02": {
              "hours_rwk": "False",
              "hours_st": "4L",
              "hours_ewo_id": "",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          }
        }
      },
      "name": "CirilloBetancourt",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "",
      "signed_in": "True",
      "time_in": "08: 30",
      "time_out": "16: 30",
      "signed_out": "True"
    },
    "151243": {
      "jobs": {},
      "name": "JasonDiaz",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "151248": {
      "jobs": {},
      "name": "AngelTorresMadrigal",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "151336": {
      "jobs": {},
      "name": "DanielFrey",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "151419": {
      "jobs": {
        "D0-C14009-CW1": {
          "05-9033": {
            "G-07": {
              "hours_rwk": "False",
              "hours_st": "8L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          }
        },
        "D0-C14008-CW": {
          "05-9031": {
            "B-02": {
              "hours_rwk": "False",
              "hours_st": "0L",
              "hours_ewo_id": "0",
              "hours_ewo": "True",
              "hours_dt": "0L",
              "hours_ot": "2L",
              "comments": ""
            }
          }
        }
      },
      "name": "MikeGullan",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9990666": {
      "jobs": {},
      "name": "MattStaricha",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9992610": {
      "jobs": {},
      "name": "MatthewBigelow",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9992778": {
      "jobs": {},
      "name": "ZhouTan",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9993273": {
      "jobs": {},
      "name": "RandyFarinelli",
      "site_id": "D0-C14008-CW",
      "trade": "Carpenter",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996467": {
      "jobs": {},
      "name": "AlvaroSandoval(Guardado)",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996763": {
      "jobs": {},
      "name": "JesusZarate",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996767": {
      "jobs": {},
      "name": "JorgeRomero",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996768": {
      "jobs": {
        "D0-C14008-CW": {
          "05-9032": {
            "G-05": {
              "hours_rwk": "False",
              "hours_st": "3L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          },
          "05-9031": {
            "B-02": {
              "hours_rwk": "False",
              "hours_st": "5L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          }
        }
      },
      "name": "VictorRuiz",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9996771": {
      "jobs": {},
      "name": "GabinoMata-Neri",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9999247": {
      "jobs": {
        "D0-C14008-CW": {
          "05-9032": {
            "G-05": {
              "hours_rwk": "False",
              "hours_st": "8L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "0L",
              "comments": ""
            }
          },
          "05-9031": {
            "B-02": {
              "hours_rwk": "True",
              "hours_st": "0L",
              "hours_ewo_id": "0",
              "hours_ewo": "False",
              "hours_dt": "0L",
              "hours_ot": "1L",
              "comments": ""
            }
          }
        }
      },
      "name": "FranciscoMontez",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    },
    "9999314": {
      "jobs": {},
      "name": "RomanHernandez",
      "site_id": "D0-C14008-CW",
      "trade": "Mason",
      "note": "nowork",
      "signed_in": "False",
      "time_in": "06: 00",
      "time_out": "06: 00",
      "signed_out": "False"
    }
  }
]

.h file

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@property (nonatomic, strong) NSArray *nameArray;
@property (nonatomic, strong) NSArray *dict;
@property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) NSString *filePath;

@end

.m file

#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    NSError *error = nil;

    _filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
    _data = [NSData dataWithContentsOfFile:_filePath];
    _dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
    return _dict.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell" forIndexPath:indexPath];

    // Configure the cell...
    NSDictionary *dict = _dict[indexPath.row];
NSString *name = dict[@"name"];
cell.titleLabel.text = name;

    return cell;
}
2
  • Please share the main JSON file from start to some elemnts Commented Sep 21, 2015 at 10:44
  • in _nameArray, how many counts you get and what the result u get Commented Sep 21, 2015 at 11:23

5 Answers 5

3

Your JSON is actually misformatted. It should probably be:

[
    {
        "name": "XYZ",
        "details": "XYZ"
    },
    {
        "name": "XYZ",
        "details": "XYZ"
    }
]

When you are executing _dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error]; what you actually get back is an array of dictionaries and not a single dictionary. So calling _nameArray = [_dict objectForKey:@"name"]; should actually return nil or crash.

Here's what your implementation should look like:

#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error = nil;
    _filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
    _data = [NSData dataWithContentsOfFile:_filePath];
    // Remember here, __dict is an array, I kept the same name as your code but it is NOT a dictionary.
    _dict = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error];
    _nameArray = [_dict objectForKey:@"name"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dict.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell" forIndexPath:indexPath];
    // Remember here, __dict is an array, I kept the same name as your code but it is NOT a dictionary.
    NSDictionary *dict = _dict[indexPath.row];
    NSString *name = dict[@"name"];
    cell.titleLabel.text = name;
    return cell;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I changed dict from NSDictionary to NSArray in my .h file, made the necessary changes to my code, but the table is still empty!
Did you connect the controller as the table view data source and delegate?
I updated the post with the main json file. Can you see why the data doesn't show up now?
@iFlames the problem is that the top container of your JSON is an array that contains a giant dictionary of all values. This means that your table view must deal with a data source that is a dictionary and not an array. Basically, the top array container is useless in a way, you need to ignore it and deal with only the dictionary inside of it.
0

key "name" will not return you an array. It will return instead a NSString.

Comments

0

Your JSON seems to array of dictionaries. So this is how you would fix your issue:

While parsing data from JSON, save in NSArray:

@property (nonatomic, strong) NSArray *jsonArray;
self.jsonArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 

And then while reading data in your cell, read it like this:

NSDictionary *nameDict = self.jsonArray[0];
NSLog(@"Name = %@", nameDict[@"name"]); // This prints Name = XYZ

Comments

0

The actual format of .json file should be like this:

[
  {
      "name": "XYZ",
      "details": "XYZ"
  },
  {
      "name": "XYZ",
      "details": "XYZ"
  }
]

Now your .m file should be as below:

NSError *error = nil;

_filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
_data = [NSData dataWithContentsOfFile:_filePath];
_dataArray = [NSJSONSerialization JSONObjectWithData:_data options:0 error:&error];


//_nameArray = [_dict objectForKey:@"name"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell" forIndexPath:indexPath];

    NSDictionary *dict = [self.nameArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = dict[@“name”];


    return cell;
}

Hope this helps.

Comments

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell" forIndexPath:indexPath];

    // Configure the cell...
        NSString *name = [self.nameArray objectAtIndex:indexPath.row];
        cell.titleLabel.text = name;


    //NSString *desc = [self.results objectForKey:@""];

    return cell;
}

1 Comment

Hey @iFlames check my answer, just replace below code. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.