AFNetworking 解析json 数据

创建 Model 类

定义属性


-(void)loadData{


    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];

    manger.responseSerializer = [AFHTTPResponseSerializer serializer];

    [mangerGET:@"http://127.0.0.1/Book.json"parameters:nilprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSLog(@"成功");


    NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:responseObjectoptions:1error:nil];

        for(NSString* keyindic) {

            [self.titleArrayaddObject:key];

            NSMutableArray *arr = [[NSMutableArray alloc]init];

            for(NSDictionary*dictindic[key]) {


                BookModel*model = [[BookModelalloc]init];

                [modelsetValuesForKeysWithDictionary:dict];

                [arraddObject:model];

            }

            [self.arrayaddObject:arr];

        }


        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tbVreloadData];

        });


    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        NSLog(@"失败");

    }];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return self.titleArray.count;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return[self.array[section]count];

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];


    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }



    cell.textLabel.text= [self.array[indexPath.section][indexPath.row]title];

    cell.detailTextLabel.text=[NSStringstringWithFormat:@"作者:%@ 价格%@",[self.array[indexPath.section][indexPath.row]author],[self.array[indexPath.section][indexPath.row]price]];


    returncell;

}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

    returnself.titleArray[section];

}

你可能感兴趣的:(AFNetworking 解析json 数据)