声明老师的一些属性
及时释放
开始解析
#import <Foundation/Foundation.h>
@interface TeacherModel : NSObject
@property (nonatomic, retain)NSString *name;
@property (nonatomic, retain)NSString *sex;
@property (nonatomic, retain)NSString *number;
@property (nonatomic, retain)NSString *phone;
@end
及时释放
#import "TeacherModel.h"
@implementation TeacherModel
- (void)dealloc
{
[_name release];
[_sex release];
[_phone release];
[_number release];
[super dealloc];
}
// KVC 进行赋值
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
声明城市的一些属性
#import <Foundation/Foundation.h>
@interface CityModel : NSObject
@property (nonatomic, retain)NSString *province;
@property (nonatomic, retain)NSArray *cityList;
@end
及时释放
#import "CityModel.h"
@implementation CityModel
-(void)dealloc
{
[_province release];
[_cityList release];
[super dealloc];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
开始解析
// json 解析老师数据
- (void)setUpJson
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Teacher" ofType:@"json"];
// 变成data
NSData *data = [NSData dataWithContentsOfFile:path];
// data转化成可变数组或者可变字典
// 是字典还是数组要看json最外层是什么
// NSJSONSerialization json使用的类
NSError *error = nil;
NSMutableArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
// NSLog(@"%@", dataArray);
// 初始化
self.dataArray = [NSMutableArray array];
// 剥香蕉
for (NSDictionary *dic in dataArray) {
TeacherModel *model = [[TeacherModel alloc] init];
[model setValuesForKeysWithDictionary:dic];
[self.dataArray addObject:model];
[model release];
}
}
// 解析城市数据
- (void)setUpDataJsonCity
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];
// 把data转化成可变数组或者字典
NSMutableArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
// 初始化数组
self.dataArray = [NSMutableArray array];
// 剥香蕉
for (NSDictionary *dic in dataArray) {
CityModel *model = [[CityModel alloc] init];
[model setValuesForKeysWithDictionary:dic];
[self.dataArray addObject:model];
}
}
老师以及部分城市信息数据
[
{"number" : 1,
"name" : "留老师",
"sex" : "男",
"phone" : "808"
},
{"number" : 2,
"name" : "王老师",
"sex" : "男",
"phone" : "807"
},
{"number" : 3,
"name" : "长老师",
"sex" : "女",
"phone" : "810"
} [
{
"province": "河北省",
"cityList": [
"石家庄市",
"唐山市",
"秦皇岛市",
"邯郸市",
"邢台市",
"保定市",
"张家口市",
"承德市",
"沧州市",
"廊坊市",
"衡水市"
]
},
{
"province": "山西省",
"cityList": [
"太原市",
"大同市",
"阳泉市",