Ios 使用JSON和AFN字典用数据填充tableview

Ios 使用JSON和AFN字典用数据填充tableview,ios,objective-c,json,uitableview,Ios,Objective C,Json,Uitableview,这里是c&ios n00b 我已经研究这个问题三天了。我担心我缺少一个基本概念。我已经研究并完成了与此相关的所有教程和堆栈溢出问题,但我无法得到答案。我试图用json文件中的数据填充tableviewcontroller。我可以连接JSON文件并从中输出,但我似乎无法解析字典并实际使用字典。以下是我认为最接近正确的代码: TABLEVIEW CONTROLLER #import "AllTableViewController.h" #import "AFHTTPRequestOperation.

这里是c&ios n00b

我已经研究这个问题三天了。我担心我缺少一个基本概念。我已经研究并完成了与此相关的所有教程和堆栈溢出问题,但我无法得到答案。我试图用json文件中的数据填充tableviewcontroller。我可以连接JSON文件并从中输出,但我似乎无法解析字典并实际使用字典。以下是我认为最接近正确的代码:

TABLEVIEW CONTROLLER
#import "AllTableViewController.h"
#import "AFHTTPRequestOperation.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "Beer.h"


@interface AllTableViewController ()

@end

@implementation AllTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (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;
    NSURL *URL = [NSURL URLWithString:@"http://www.hopshack.com/db_get_all_beer.php"];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject[@"beer"][3][@"name"]);
       NSMutableArray *tempArray=[[NSMutableArray alloc]init];
        for(NSDictionary *oneDictionary in responseObject){
            Beer *newBeer=[[Beer alloc]initWithName:oneDictionary[@"beer"][@"name"]];
            [tempArray addObject:newBeer];
        }
        self.beers=[[NSArray alloc]initWithArray:tempArray];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Beer"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];
    [operation start];


}

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



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

    // Return the number of sections.
    return 1;
}

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


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

    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"pCell"];
    }
    cell.textLabel.text=[self.beers[indexPath.row] name];
    return cell;
}
BEER MODEL
#import "Beer.h"
#import "AFHTTPRequestOperation.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"

@implementation Beer
-(id)initWithName:(NSString *)aName{
    self=[super init];
    if(self){
        self.name=aName;
    }
    return self;
}

@end
提前感谢你们的帮助。这个网站的规则

如果您是self.beers数组已正确填充,那么我认为您没有重新加载tableview

在self.beers=[[NSArray alloc]initWithArray:tempArray]之后添加此行

它将在您收到所有数据后重新加载tableview。 如果这不起作用,那么NSLog%@,自助啤酒;为了确保自己。啤酒很受欢迎

在实际填充tableView之前,我没有意识到它会给您一个错误。我认为问题在于名称返回的是NSString而不是NSArray。实际上,responseObject包含一个字典数组,因此当您遍历该数组时,可以为每个啤酒获得一个字典。这样,您就可以使用objectForKey访问啤酒名称

检索啤酒的名称


让我知道它是否有效。

Yan,谢谢你的帮助,但这给我带来了与我得到的运行时错误基本相同的错误:`-[\u NSCFString objectForKeyedSubscript::]:未识别的选择器发送到实例0x8cb3b20 2014-06-08 12:34:53.580 HopShack[491:60b]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFString objectForKeyedSubscript:]:无法识别的选择器发送到实例0x8cb3b20`有什么建议吗?Yan,另外,通过一个断点说,当我将对象添加到临时数组中时,我的应用程序就崩溃了,就像你没有字典,你有字符串一样。尝试调试响应中返回的数据。也可以使用格式化响应。这样,您将看到所有键a值的类型。
dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData]
            });
Beer *newBeer=[[Beer alloc]initWithName:[oneDictionary objectForKey@"name"]];