{ "jsonNode": [ {"ID":"1","name":"A"}, {"ID":"2","name":"B"}] }
#import "SBJson.h"
#import "ViewController.h"
#import "SBJson.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initParseJSONData];
}
- (void)initParseJSONData
{
//parseJSONData
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"jsonfile" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSString *jsonReturn = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *jSON = [parser objectWithString:jsonReturn error:nil];
NSMutableArray * jsonData = [jSON objectForKey:@"jsonNode"];
NSLog(@"jsonFindData %@",jsonData);
CellItem = [jsonData valueForKey:@"name"] ;
NSLog(@"jsonFindItem: %@",CellItem);
}
readJSON[4392:f803] jsonFindItem: (
A,
B
)
與Table結合
.h
#import.m@interface jsonTableView : UITableViewController { NSArray *CellItem ; }
#import "SBJson.h"
#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 [CellItem count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Celldata";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = [CellItem objectAtIndex:indexPath.row];
return cell;
}
Cell 上為Label的版本
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Celldata";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
// cell.textLabel.text = [CellItem objectAtIndex:indexPath.row];
NSString *text = [CellItem objectAtIndex:indexPath.row];
// NSString *textLong = [[NSString alloc] initWithFormat: @"%@: %@",[CellItemID objectAtIndex:indexPath.row], [CellItem objectAtIndex:indexPath.row]];
UILabel *cellLabel = (UILabel *) [cell viewWithTag:2] ; //Label設Tag 2
cellLabel.lineBreakMode = UILineBreakModeWordWrap; //設置換行
cellLabel.numberOfLines = 0; //設置自動行數
[cellLabel setText:text];
return cell;
}
最外層非NSDictionary格式
[ {"ID":"1","name":"A"}, {"ID":"2","name":"B"} ]
- (void)viewDidLoad
{
[super viewDidLoad];
[self initParseJSONData];
}
- (void)initParseJSONData
{
//parseJSONData
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"jsonfile" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSString *jsonReturn = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *jSON = [parser objectWithString:jsonReturn error:nil];
CellItem = jSON ;
NSLog(@"jsonContent: %@",jSON);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Celldata";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
// cell.textLabel.text = [CellItem objectAtIndex:indexPath.row];
NSDictionary *jsonData = [CellItem objectAtIndex: indexPath.row];
UILabel *cellLabel = (UILabel *) [cell viewWithTag:2] ;
cellLabel.lineBreakMode = UILineBreakModeWordWrap; //設置換行
cellLabel.numberOfLines = 0; //設置自動行數
[cellLabel setText:[jsonData objectForKey: @"name"]];
return cell;
}
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。