#在蓝懿学习iOS的日子#day17

1、解析Json,新建一个类的对象Json
+(NSMutableArray*)parserMenusWithDic:(NSDictionary *)dic{
    //对照TEXT解析得到菜单  {}是字典[]是数组
    NSDictionary *resultDic = dic[@"result"];
    NSArray *dataArr = resultDic[@"data"];
    NSMutableArray *menus = [NSMutableArray array];
    //遍历数组得到里面的内容
    for (NSDictionary*menuDic in dataArr) {
        Menu*m = [[Menu alloc]init];
        m.number = menuDic[@"number"];
        m.title = menuDic[@"title"];
        m.imtro = menuDic[@"imtro"];
        m.ingredients = menuDic[@"ingredients"];
        m.burden = menuDic[@"burden"];
        //albums是一张图片
        m.albums = menuDic[@"albums"][0];
          [menus addObject:m];
    }
    return menus;
}
2、data
  //data 表示二进制数据的对象
    NSData *data = [NSData dataWithContentsOfFile:@"/Users/ivan/Documents/第二期/代码/day17/Day17Json/Day17Json/menus.txt"];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//    获取出辣椒炒肉
//    NSDictionary *resultDic = dic[@"result"];
//    NSArray *dataArr = resultDic[@"data"];
//    NSDictionary *firstCaiDic = dataArr[0];
//    NSString *name = firstCaiDic[@"title"];
//    NSLog(@"%@",name);

    //数组menus已在JsonParser已经初始化
    self.menus = [JsonParser parseMenusWithDic:dic];
3.文本的图片来自网络,如何传进代码里
1、编写代码
 NSData*data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.menu.albums]];
    self.albumsIV.image = [UIImage imageWithData:data];
2、另外点击 点击进入OpenAs点击再进入source code,在源代码修改添加红框的内容点击进入OpenAs点击再进入source code,在源代码修改添加红框的内容
posted @ 2015-11-12 17:28  一笑抿江湖  阅读(152)  评论(0编辑  收藏  举报