AFNetworking3.0.4和JSONModel1.0.2的基本使用

来源:互联网 发布:阿里云ssl不备案 编辑:程序博客网 时间:2024/05/18 04:48

最近公司在做一个新项目,在这里总结一下AFNetWorking和JSONModel的配合使用。

AFNetWorking是进行网络数据的请求,请求下来的一般是JSON格式的网络数据,JSON格式即为字典格式,即key和value一一对应。 有了JSON格式的网络数据之后,再使用JSONModel,将JSON转换成model,这样就能用“model.”来调用任意的key,来取出你想要的value值进行使用。

在这里使用的是聚合数据提供的天气预报的API,
http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=d162fbd409bfff09e7f2aca6a3948dd6
解析之后的JSON格式即为:
这里写图片描述

废话不多说,直接上代码

UserModel.h

#import <JSONModel/JSONModel.h>//次次model@interface ThreeModel : JSONModel@property (nonatomic,strong) NSString *temp;@property (nonatomic, strong) NSString *wind_direction;@end// 次model@interface ScoreModel : JSONModel@property (nonatomic, strong) ThreeModel *sk;@property (nonatomic, strong) NSArray *future;// model 嵌套数组@end// 主model@interface UserModel : JSONModel@property (nonatomic, strong) NSString *resultcode;       // 一般@property (nonatomic, strong) ScoreModel *result;  // model 嵌套 model@end

Usermodel.m

#import "UserModel.h"#import <JSONModel/JSONModel.h>@implementation ThreeModel+ (BOOL)propertyIsOptional:(NSString *)propertyName{    return YES;}@end@implementation ScoreModel+ (BOOL)propertyIsOptional:(NSString *)propertyName{    return YES; //}@end@implementation UserModel+ (BOOL)propertyIsOptional:(NSString *)propertyName{    return YES;}@end

ViewController.m

#import "ViewController.h"#import <JSONModel/JSONModel.h> //JSONModel第三方库#import "AFNetworking.h"//主要用于网络请求方法#import "UserModel.h"@interface ViewController ()@property(nonatomic,strong) NSString* name;@property(nonatomic,strong) NSMutableArray* Labels;@property (nonatomic, strong) UIButton *button;@property (nonatomic,strong) NSDictionary *NIU;@property (nonatomic,strong) NSString *labelText;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];       //设置VIEW的背景色;    self.view.backgroundColor = [UIColor whiteColor];    [self Model];   }- (void)Model{    NSURL *URL = [NSURL URLWithString:@"http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=d162fbd409bfff09e7f2aca6a3948dd6"];    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    [manager GET:URL.absoluteString parameters:nil success:^(NSURLSessionTask *task, id responseObject) {        NSLog(@"%@", responseObject);       //网络请求是异步操作,初始化model的时候数据还没请求到,所以请求和解析必须在一起。  model中数据的使用也是这个道理,不能直接给label等赋值,        UserModel *model = [[UserModel alloc] initWithDictionary:responseObject error:nil];        NSLog(@"%@",model.resultcode);// 正常的 字典        NSLog(@"%@",model.result.sk.wind_direction);        NSLog(@"%@",model.result.future[0]);// 嵌套 model数组        _labelText = model.resultcode;        [self UI]; //自定义的UI控件,用来显示model中的数据    } failure:^(NSURLSessionTask *operation, NSError *error) {        NSLog(@"Error: %@", error);    }];}- (void)UI{    //设置label控件的位置    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 50)];    lable.backgroundColor = [UIColor blueColor];    //label文字居中    lable.textAlignment = UITextAlignmentCenter;    lable.text = _labelText; //model中的数据赋值给label    lable.textColor = [UIColor redColor];    //把label控件添加到当前的view上    [self.view addSubview:lable];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

总结:
JSONModel使用的三步走:
一、用bejson在线解析URL的数据格式,弄清楚JSON数据各级的层级关系;
二、按层级关系声明自己需要的model模型的属性。(只要层级关系声明正确了,一般都能解析出来)
三、注意initWithDictionary方法的使用,这是JSONModel里最重要的一个方法。 用UserModel *model = [[UserModel alloc] initWithDictionary:responseObject error:nil]; 将字典型JSON数据转换成Model。
四、给将model里你需要的数据赋值给全局变量,然后再UI控件中显示。注意,先解析,后赋值给UI控件。

上面提到的AFNnetworking请求网络数据,使用的是GET方式的请求,下面说一下POST方式的请求,其实是大同小异。
还是直接上代码:

- (void)Model{//http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=d162fbd409bfff09e7f2aca6a3948dd6    NSURL *URL = [NSURL URLWithString:@"http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&"];    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    //假如需要提交给服务器的参数是key=1,class_id=100    //创建一个可变字典    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];    //往字典里面添加需要提交的参数    [parametersDic setObject:@"d162fbd409bfff09e7f2aca6a3948dd6" forKey:@"key"];    //以post的形式提交,POST的参数就是上面的域名,parameters的参数是一个字典类型,将上面的字典作为它的参数    [manager POST:URL.absoluteString parameters:parametersDic success:^(NSURLSessionTask *task, id responseObject) {        NSLog(@"%@", responseObject);    } failure:^(NSURLSessionTask *operation, NSError *error) {        NSLog(@"Error: %@", error);    }];}

以上就是AFNetworking和JSONModel的基本使用。 这里说的AFNetworking是AFNetworking3.0.4和3.0之前的版本在方法上的使用有很多区别。

再贴两个关于这方面有用的链接:
http://www.jianshu.com/p/1c0f131715ac

http://www.jianshu.com/p/047463a7ce9b

0 0