您的位置:首页 > 移动开发 > IOS开发

iOS-第三方库MJExtension使用 解析Json数据

2016-02-16 00:00 731 查看
//NSObject 是基于runtime框架编写

//简单的json 数据转model
NSDictionary *dic=@{@"username":@"1510",@"des":@"1510棒棒的,胖胖的"};
UserModel *model=[[UserModel alloc]mj_setKeyValues:dic];

// 模型装数据
NSDictionary *dic1=[model mj_keyValues];

//  ==========复杂层次的数据.
NSDictionary *subUser=@{@"subName":@"1510son",@"age":@"4"};
NSDictionary *dic2=@{@"username":@"1510",@"des":@"1510棒棒的,胖胖的",@"subUser":subUser};

UserModel *model2=[[UserModel alloc]mj_setKeyValues:dic2];
//跨级解析 跨级的数据要求为Dictionary类型

//    NSLog(@"%@",model2.subUser.subName);
//  数据模型转数组模型
NSMutableArray *modelList=[NSMutableArray new];

for (int i=0; i<10; i++) {
[modelList addObject:dic2];
}

NSArray *list=[UserModel mj_objectArrayWithKeyValuesArray:modelList];

// 解析网络Json数据
NSURL *url=[NSURL URLWithString:@"http://1000phone.net:8088/app/iAppFree/api/limited.php?page=1&number=10"];
NSData *data=[NSData dataWithContentsOfURL:url];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

// 直接解析网络数据到数据模型里面
XianMianModel *model=[[XianMianModel alloc]mj_setKeyValues:dic];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息