1

ビューコントローラー内で、jsonをテーブルビューに解析しようとしています。しかし、ログに記録しようとしても表示されません。また、この 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"}
]
}

前もって感謝します!

4

2 に答える 2

3

JSON に格納されている内容に応じて、NSJSONSerializer を使用して JSON を NSArray または NSDictionary にデコードする必要があります。例えば:

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

私の場合、JSON は一連の辞書です。このコードは、辞書を取得して配列に格納します。私は1つをつかむことができます:

NSDictionary *dict = [jsonArray objectAtIndex:0;

そして、その辞書を使用します。

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

基本的に、データを使用して、使用する他のオブジェクトと同じようにセルにデータを入力できます。

于 2013-01-14T17:10:11.697 に答える
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];
}
于 2013-01-17T18:17:58.247 に答える