// // chonViewController.h // TestJson // // Created by choni on 14-5-16. // Copyright (c) 2014年 choni. All rights reserved. // #import <UIKit/UIKit.h> @interface chonViewController : UITableViewController //保存數據列表 @property(nonatomic,strong) NSMutableArray * listData; @end
與之對應的 chonViewController.m文件 代碼以下:json
// // chonViewController.m // TestJson // // Created by choni on 14-5-16. // Copyright (c) 2014年 choni. All rights reserved. // #import "chonViewController.h" @interface chonViewController () @end @implementation chonViewController - (void)viewDidLoad { [super viewDidLoad]; NSString * path = [[NSBundle mainBundle]pathForResource:@"Notes" ofType:@"json" ]; NSData * jsonD
NSData * jsonData = [[NSData alloc] initWithContentsOfFile:path]; NSError * error ; id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; if (!jsonObj || error) { NSLog(@"JSON解析失敗"); } self.listData = [jsonObj objectForKey:@"Record"]; } #pragma mark - tableView -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.listData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; NSMutableDictionary * dict = self.listData[indexPath.row]; cell.textLabel.text = [dict objectForKey:@"Content"]; cell.detailTextLabel.text = [dict objectForKey:@"CDate"]; return cell ; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end