您的位置:首页 > 理论基础 > 计算机网络

网络:JSONKit框架的使用(天气预报)

2016-04-07 15:50 525 查看
#import "ViewController.h"
#import "JSONKit.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
}

/**
1. 使用系统的解析json 效率明显比 jsonkit 会快,而且快很多
2. 如果旧项目中遇到使用解析json的第三方框架,尽量改成用系统的(AFNetworking 也是使用系统的)
3. 如果修改,可以按以下步骤:
3.1 删除JSONKit.h 和 JSONKit.m
3.2 哪里出错改哪里

*/
- (void)JSONKit {
NSURL *url = [NSURL URLWithString:@"http://localhost/demo.json"];

NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:0 timeoutInterval:10];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
for (int i = 0; i< 100 * 1000; i++) {
id result = [[JSONDecoder decoder] objectWithData:data];
}
NSLog(@"jsonkit %f",CFAbsoluteTimeGetCurrent() - start);
}];
}

- (void)loadData {
NSURL *url = [NSURL URLWithString:@"http://localhost/demo.json"];

NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:0 timeoutInterval:10];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
for (int i = 0; i< 100 * 1000; i++) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
}
NSLog(@"jsonkit %f",CFAbsoluteTimeGetCurrent() - start);
}];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: