iOS-UI-JOSN数据解析-第三方方法

2016-02-29 17:03:56





//
// ViewController.m
// JOSN解析
//
// Created by YIem on 16/2/29.
// Copyright © 2016年 YIem. All rights reserved.
//

import "ViewController.h"

import "Student.h"

import "JSONKit.h"

@interface ViewController ()
@property (nonatomic, retain) NSMutableArray *stuArr;
@end

@implementation ViewController

  • (void)dealloc
    {
    [_stuArr release];
    [super dealloc];
    }
  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *jsonB = [UIButton buttonWithType:UIButtonTypeSystem];
    jsonB.frame = CGRectMake(100, 100, 80, 30);
    [jsonB setTitle:@"JSON解析" forState:UIControlStateNormal];
    [jsonB addTarget:self action:@selector(jsonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:jsonB];

    }

  • (void)jsonAction
    {
    // 获取文件路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"txt"];
    // 读取文件内容
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    // 第三方解析
    NSMutableArray *arr = [data objectFromJSONData];

    NSLog(@"%@", arr);
    self.stuArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {

    Student *stu = [[Student alloc] init];
    [stu setValuesForKeysWithDictionary:dic];
    [self.stuArr addObject:stu];

    }

}

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    }

@end



//
// Student.h
// JOSN解析
//
// Created by YIem on 16/2/29.
// Copyright © 2016年 YIem. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, copy) NSString *number;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *sex;
@property (nonatomic, copy) NSString *phone;
@end


//
// Student.m
// JOSN解析
//
// Created by YIem on 16/2/29.
// Copyright © 2016年 YIem. All rights reserved.
//

import "Student.h"

@implementation Student

  • (void)dealloc
    {

    [_number release];
    [_name release];
    [_sex release];
    [_phone release];
    [super dealloc];

    }

@end



导入第三方文件

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »