#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
网络:JSONKit框架的使用(天气预报)
最新推荐文章于 2021-01-12 20:02:30 发布