如何从URL解析嵌套JSON?

时间:2015-03-07 01:08:04

标签: ios json uitableview datamodel

尝试解析嵌套的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

2 个答案:

答案 0 :(得分:0)

问题与错误描述完全一样。您尝试在isEqualToString:上拨打NSArray,这会导致崩溃。我不确定你在哪里进行比较,但它正在发生。这可能是因为您在解析JSON而不是valueForKey:时使用objectForKey:这一事实。

答案 1 :(得分:0)

这很棘手。我要感谢Hot Licks!

将JSONData上的postVal = valueForKey转换为数组。

我按照以下方式遍历字典数组

for (NSMutableDictionary *postDict in postVal) {}

在那里我想要字典中的字段。所以我使用了以下内容:

id key;
while((key = [enumerator nextObject])) {}

之后,所有键都是字符串,但值是数组。 为了将数组作为字符串,我使用了以下

NSString * bName = [fieldVal objectForKey:key][0];

Type终于是NSString !!!!!!!

谢谢。