2011-05-16 122 views
1

我是iPhone應用開發新手。 我需要一些幫助...我想解析一些數據(銀行名稱)並將它們顯示爲UITableView。我使用這個代碼來解析銀行名稱這是JSON格式:iPhone JSON解析

 NSArray *atmDAta = [responseString JSONValue]; 
for (NSDictionary *dict in atmDAta) { 
    NSLog(@"%@", [dict objectForKey:@"Name"]); 
    [listOfItems addObject:[dict objectForKey:@"Name"]]; 
} 

但是當我嘗試向他們展示在表是這樣的:

cell.textLabel.text = [listOfItems objectAtIndex:indexPath.row]; 
return cell; 

這會告訴我沒什麼表但NSlog正在工作,並顯示我在xcode日誌中的銀行名稱。請幫忙。

+0

莫非你一些你的代碼? ListOfItem是否保持正確?當你完成分析時,它是否有任何項目?或者是你插入一個空字符串的名字? – rckoenes 2011-05-16 10:28:32

+0

'code' NSMutableArray * listOfItems; – Rocky 2011-05-16 10:33:04

+0

Yaeh我保留正確 – Rocky 2011-05-16 10:40:52

回答

0

呼叫[yourtable reloadData];當你完成JSON解析

+0

是的,我也這樣做[atmList reloadData];但仍然沒有任何輸出 – Rocky 2011-05-16 10:29:48

+0

你可以發佈一些更多的代碼或粘貼從你得到的JSON鏈接 – 2011-05-16 10:31:45

+0

這是[鏈接](http://www.fourspan.com/atminfo/getatminfo.php?lat=47.822828 &long = -28.44989)從我解析數據的位置 – Rocky 2011-05-16 10:36:05

0

請參考此代碼爲JSON解析

-(void)getLatestScore 
{ 
    SBJsonParser *parser = [[SBJsonParser alloc] init];  

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://json-cricket.appspot.com/score.json"]]; 

    // Perform request and get JSON back as a NSData object 
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    // Get JSON as a NSString from NSData response 
    NSString *str_Json_String = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

    // parse the JSON response into an object 

    NSDictionary *dicCricketScore = [parser objectWithString:str_Json_String error:nil]; 

    lblDate.text = [dicCricketScore objectForKey:@"date"]; 
    lblMatch.text = [dicCricketScore objectForKey:@"match"]; 
    lblBattingTeam.text = [dicCricketScore objectForKey:@"batting_team"]; 
    lblScore.text = [dicCricketScore objectForKey:@"score"]; 
    lblSummary.text = [dicCricketScore objectForKey:@"summary"]; 
} 

從這個代碼,你會從JSON解析得到最新板球得分....

+0

你好Mehul Mistry ......很多thanX這個,但我已經解析數據到標籤....現在我只需要解析數據並將其顯示到UITableView中 – Rocky 2011-05-16 10:53:13

+0

你在哪裏存儲數據...... NSMutableArray或其他... – 2011-05-16 11:01:50

0

@ Rocky試試這個試試

聲明在.h NSArray * listOfItems;

@property (nonatomic, copy)NSArray *listOfItems; 

,並使用這.M

NSMutableArray *tempArray = [[NSmutableArray alloc]initWithCapacity:[atmDAta count]]; 
for (NSDictionary *dict in atmDAta) { 
    NSLog(@"%@", [dict objectForKey:@"Name"]); 
    if (![dict valueForKeyIsNull:@"Name"]) { 
     [tempArray addObject:[dict objectForKey:@"Name"]];   
    } 
} 
self.listOfItems = [NSArray arrayWithArray:tempArray]; 
[tempArray release]; 
if([self.listOfItems count] > 0) 
[yourTable reloadData]; 

當我使用的是分類

@implementation NSDictionary (KeyExists) 

- (BOOL) keyExists:(NSString *) key { 
    return [self valueForKey:key] != nil; 
} 

- (BOOL) valueForKeyIsNull:(NSString *) key { 
    return ![self keyExists:key] || ((NSNull *)[self valueForKey:key] == [NSNull null]); 
} 
@end 

乾杯!

0

加入數組,你可以做後[LISTOFITEMS保留]