在Viewcontroller中将JSON解析为Tableview

时间:2013-01-14 16:30:47

标签: iphone json tableview

我试图在viewController中解析json到我的tableview。但是当我试图记录它时,它不会出现。我也想知道如何把这个JSON放到我的tableviewcells中。

自从我第一次提出这个问题以来,我改变了我的代码。

请参阅下面的代码:

的.m:

#import "UbaGuideStartViewController.h"

#import <QuartzCore/QuartzCore.h>





@interface UbaGuideStartViewController (){
    NSMutableData *jsonData;
    NSURLConnection *connection;
    NSMutableArray *arrData;
}


@property (weak, nonatomic) IBOutlet UIImageView *ImgHeader;


@property (weak, nonatomic) IBOutlet UIImageView *ImgTitle;



@end

@implementation UbaGuideStartViewController



 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [[self StartTableView]setDelegate:self];
    [[self StartTableView]setDataSource:self];
    arrData = [[NSMutableArray alloc]init];



}

//Json parse.

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse  *)response
{
    [jsonData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [jsonData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Failed with error");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{
    NSDictionary *guideDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

    NSArray *arrguideTitle = [guideDictionary objectForKey:@"guide"];

    for (NSDictionary *dictionary in arrguideTitle)
    {
        NSString *guideTitle = [dictionary objectForKey: @"guideTitle"];

        [arrData addObject:guideTitle];
    }
    [self.StartTableView reloadData];

    NSURL *url = [NSURL   URLWithString:@"http://www.sofisto.com.br/public_html/TEST/guide.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if (connection){
        jsonData = [[NSMutableArray alloc]init];
    }
}


//TableView

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *startGuideCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (startGuideCell == nil) {
        startGuideCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // configure your cell here...

    startGuideCell.textLabel.text = [arrData objectAtIndex:indexPath.row];



    return startGuideCell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}




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


@end

也是我的JSON:

{
"guide": [
{ "guideTitle":"Where to stay"}, 
{ "guideTitle":"Where to eat"}, 
{ "guideTitle":"What to do"}
]
}

提前致谢!

2 个答案:

答案 0 :(得分:3)

您需要使用NSJSONSerializer将JSON解码为NSArray或NSDictionary,具体取决于存储在JSON中的内容。例如:

NSError *error;
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

就我而言,JSON是一系列词典。 此代码获取字典并将它们存储到数组中。我可以抓住一个:

NSDictionary *dict = [jsonArray objectAtIndex:0;

然后使用该字典;

NSString *string = [dict objectForKey:@"someKey"];

基本上,您可以像使用其他任何对象一样使用数据来填充单元格。

答案 1 :(得分:0)

不确定这是否是故意的,但您是在connectionDidFinishLoading回调中创建并启动连接。

尝试在viewDidAppear或类似的生命周期回调方法中启动连接(使用以下代码)。

NSURL *url = [NSURL   URLWithString:@"http://www.sofisto.com.br/public_html/TEST/guide.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

connection = [NSURLConnection connectionWithRequest:request delegate:self];

if (connection){
    jsonData = [[NSMutableArray alloc]init];
}