JSON parsing network data

Posted by atoboldon on Sat, 04 Jan 2020 09:26:15 +0100

1. Set info to allow network requests
2. Find the interface file and run it to grimace
3.Appdelegate specifies the default root view, establishes the MVC management mode, establishes the model class, and customizes the cellXIB class, as shown in the model:

This is mainly to find the corresponding object name in the grimace interface file and define it
4. Set the location interface settings of xib in the user-defined cell, and define the properties in. h by dragging
And declare the method - (void)setValueForCellWithBook:(Book *)book in. h;
Assign value in. m, give value, realize the method of declaration

#import "BookCell.h"

@implementation BookCell

-(void)setValueForCellWithBook:(Book *)book{
    if (book) {
        self.imgV.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:book.iconUrl]]];
        self.label1.text = book.name;
        self.label2.text = book.expireDatetime;
        self.label3.text = [NSString stringWithFormat:@"share:%@second",book.shares];
        self.label4.text = [NSString stringWithFormat:@"Collection:%@second",book.favorites];
        self.label5.text = [NSString stringWithFormat:@"download:%@second",book.downloads];
        self.label6.text = book.categoryName;
        self.label7.text = book.lastPrice;

5. Complete the interface construction in the viewcontroller and request the analysis data as follows

//  ViewController.m
//  Practice JSON parsing
#import "ViewController.h"
#import "Book.h"
#import "BookCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong)UITableView *ojtable;
@property(nonatomic , strong)NSMutableArray *arr;


@implementation ViewController
- (UITableView *)ojtable{
    if (!_ojtable) {
        _ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _ojtable.delegate = self;
        _ojtable.dataSource =self;

    return _ojtable;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title= @"Price reduction";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Set up" style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"classification" style:UIBarButtonItemStyleDone target:self action:nil];
    [self.view addSubview:self.ojtable];
    [self.ojtable registerNib:[UINib nibWithNibName:@"BookCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    NSURL *url = [NSURL URLWithString:@"http://iappfree.candou.com:8080/free/applications/sales?page=1&number=1"];
    NSData *data = [NSData dataWithContentsOfURL:url];

    self.arr = [[NSMutableArray alloc]init];
    NSError *error = nil;
    NSDictionary * dic  = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray * array = dic[@"applications"];
    for (NSDictionary *dic in array) {
        Book *book = [Book new];
        [book setValuesForKeysWithDictionary:dic];
        [self.arr addObject:book];
    [self.ojtable reloadData];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.arr.count > 0) {
        [cell setValueForCellWithBook:self.arr[indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;


Topics: network JSON