【发布时间】:2015-05-08 17:36:51
【问题描述】:
试图解析嵌套的 json。试图抓取嵌套 json 文件中的内容。似乎是一个字典数组。在其中,我们想要一个包含字典内容(帖子)的字段。然后在 custom_fields 中,它是一个数组的数组。
///////////// 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
【问题讨论】:
-
如果你的数据链接是可信的,那么最外层的 JSON 结构是一个对象,而不是一个数组。此外,它不是可变的,因为您没有为此指定解析选项。然而,“posts”的内容是一个数组,在它上面使用
valueForKey会返回一个“custom_fields”值的数组,其中每个单独的值都是一个NSDictionary。为“custom_fields”数组的第 n 个条目请求“cf_Business_name”元素将返回一个 NSArray,通常包含一个名称。也就是说,您认为 NSString 是 NSArray,正如异常清楚地告诉您的那样。 -
你真的需要收拾这个烂摊子,区分哪些是数组,哪些是字典。使用
valueForKey及其亲属与以更传统的方式剥离 JSON 相比,您会造成很多浪费的动作(和混乱)。 -
如果你查看过异常堆栈跟踪,它会告诉你错误的确切位置。
-
看来 custom_fields 中的值是数组(例如 cf_Business_Name、cf_Address....)。 json的结构是:
标签: ios json uitableview datamodel