Trying to parse nested json. Trying to grab the content within the nested json file. Appears to be an array of dictionaries. Within that we want a field that has dictionary content (posts). Then within that the custom_fields which is an array of an array.
///////////// ViewController.h //////////
@property (nonatomic, strong) NSMutableArray *jsonArray;
@property (nonatomic, strong) NSMutableArray *postArray;
@property (nonatomic, strong) NSMutableDictionary *fieldArray;
@property (nonatomic, strong) NSMutableArray *testArray;
- (void) retrieveData;
@end
///////////////// ViewController.m //////////////////
#import "ViewController.h"
#import "Model.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize jsonArray, postArray, fieldArray, testArray;
- (void)viewDidLoad {
[super viewDidLoad];
//Set title of VC
self.title = @"SF";
//Load data
[self retrieveData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
// Return the number of rows in the section.
return fieldArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Configure cell:
Model * modelObject = [[Model alloc] init];
modelObject = [fieldArray objectAtIndex:indexPath.row];
cell.tag = indexPath.row;
cell.textLabel.text = modelObject.address;
NSLog(@"the text is %@", modelObject.address);
//Accessory
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void) retrieveData
{
NSURL * url = [NSURL URLWithString:getDataURL];
NSData * data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//Set up arrays
postArray = [[NSMutableArray alloc] init];
fieldArray = [[NSMutableDictionary alloc] init];
testArray = [[NSMutableArray alloc] init];
NSLog(@"old post class: %@", NSStringFromClass([postArray class]));
NSLog(@"old field class: %@", NSStringFromClass([fieldArray class]));
NSLog(@"old test class: %@", NSStringFromClass([testArray class]));
//Set up array from jsonArray
postArray = [jsonArray valueForKey:@"posts"];
fieldArray = [postArray valueForKey:@"custom_fields"];
NSLog(@"new post class: %@", NSStringFromClass([postArray class]));
NSLog(@"new field class: %@", NSStringFromClass([fieldArray class]));
NSLog(@"new test class: %@", NSStringFromClass([testArray class]));
NSLog(@"Test Array: %@", fieldArray);
//Set up json array
for (int i = 0; i < fieldArray.count; i++)
{
//Create object
NSString * name = [[fieldArray valueForKey:@"cf_name"] objectAtIndex:i];
//NSLog(@"name: %@", name);
NSString * address = [[fieldArray valueForKey:@"cf_Address"] objectAtIndex:i];
//NSLog(@"address: %@", address);
NSString * contact = [[fieldArray valueForKey:@"cf_contact"] objectAtIndex:i];
//NSLog(@"contact: %@", contact);
}
//Reload table view
[self.tableView reloadData];
}
@end