iOS激情详解之json解析

13 篇文章 0 订阅
声明老师的一些属性
#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": [
            "太原市",
            "大同市",
            "阳泉市",


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值