iOS:OC--JSON01

  • 先创建导航视图控制器
iOS:OC--JSON01_第1张图片
json(caipu).png

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    nav.navigationBar.tintColor = [UIColor whiteColor];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:44/255.0 green:185/255.0 blue:176/255.0 alpha:1];
    
    self.window.rootViewController = nav;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

ViewController.h

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)NSDictionary* dict;

@property(nonatomic,strong)UITableView* tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"cookery-book";
    [self creat];
}
-(void)creat{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册 cell
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"class"];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"caipu.txt" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];

    self.dict = [NSDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil]];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dict.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dict1 = [_dict valueForKey:@"data"];
    return dict1.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"class"];
    
    NSArray *array = [_dict valueForKey:@"data"];
    
    
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [dic objectForKey:@"food_str"];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

caipu.txt

{
"data": [
{
"collect_num": "1622",
"food_str": "大虾 葱 生姜 植物油 料酒",
"id": "8289",
"num": 1622,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg",
"title": "油焖大虾"
},
{
"collect_num": "1565",
"food_str": "猪肉 青蒜 青椒 红椒 姜片",
"id": "2127",
"num": 1565,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg",
"title": "四川回锅肉"
},
{
"collect_num": "1486",
"food_str": "QQ糖 牛奶 芒果",
"id": "30630",
"num": 1486,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg",
"title": "超简单芒果布丁"
},
{
"collect_num": "1398",
"food_str": "鲜鱼 姜 葱 蒜 花椒",
"id": "9073",
"num": 1398,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg",
"title": "家常红烧鱼"
},
{
"collect_num": "1385",
"food_str": "豆腐 新鲜红椒 青椒 葱花 油",
"id": "10097",
"num": 1385,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg",
"title": "家常煎豆腐"
},
{
"collect_num": "1321",
"food_str": "瘦猪肉 生菜 豆瓣酱 干辣椒 花椒",
"id": "10509",
"num": 1321,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg",
"title": "水煮肉片"
},
{
"collect_num": "1229",
"food_str": "银耳 苹果 红糖",
"id": "46968",
"num": 1229,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg",
"title": "红糖苹果银耳汤"
},
{
"collect_num": "1191",
"food_str": "豆腐 肉末 生抽 白糖 芝麻油",
"id": "10191",
"num": 1191,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg",
"title": "麻婆豆腐"
},
{
"collect_num": "1133",
"food_str": "大米 皮蛋 猪肉 油条 香葱",
"id": "2372",
"num": 1133,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg",
"title": "皮蛋瘦肉粥"
},
{
"collect_num": "1115",
"food_str": "红薯粉 肉 姜 蒜 花椒",
"id": "2166",
"num": 1115,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg",
"title": "蚂蚁上树"
},
{
"collect_num": "1056",
"food_str": "猪肉 红椒 黄椒 洋葱 蛋清",
"id": "2262",
"num": 1056,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg",
"title": "糖醋肉"
},
{
"collect_num": "987",
"food_str": "豆腐 木耳 胡萝卜 香葱 番茄酱",
"id": "9971",
"num": 987,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg",
"title": "鱼香豆腐"
},
{
"collect_num": "978",
"food_str": "四季豆 干辣椒 蒜头 酱油 糖",
"id": "10172",
"num": 978,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg",
"title": "干煸四季豆"
},
{
"collect_num": "888",
"food_str": "胡萝卜 肉 蛋 生抽 盐",
"id": "2685",
"num": 888,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg",
"title": "胡萝卜肉末蒸蛋"
},
{
"collect_num": "873",
"food_str": "青辣椒 大蒜 香醋 白糖 生抽",
"id": "9972",
"num": 873,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg",
"title": "虎皮青椒"
},
{
"collect_num": "787",
"food_str": "排骨 李锦记叉烧酱 植物油 清水 油菜",
"id": "10437",
"num": 787,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg",
"title": "叉烧排骨"
},
{
"collect_num": "743",
"food_str": "面条 肉丝 淀粉 酱油 辣椒",
"id": "2348",
"num": 743,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg",
"title": "麻辣肉丝面"
},
{
"collect_num": "741",
"food_str": "黑木耳 玉米 牛蒡 胡萝卜 西兰花",
"id": "2892",
"num": 741,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg",
"title": "“五行”彩蔬汤"
},
{
"collect_num": "738",
"food_str": "土豆 翅根 葱 姜 料酒",
"id": "10044",
"num": 738,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg",
"title": "土豆炖翅根"
},
{
"collect_num": "737",
"food_str": "黄豆 红豆 绿豆 黑豆 黑米",
"id": "33783",
"num": 737,
"pic": "http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg",
"title": "美人豆浆"
}
],
"ret": 1
}

你可能感兴趣的:(iOS:OC--JSON01)