json数据格式原生解析器
来源:互联网 发布:金九银十 数据 编辑:程序博客网 时间:2024/05/22 09:40
请求服务器返回数据格式无非就是两种:json和XML,这篇讲解原生如何解析json格式数据
- (void)viewDidLoad{ [super viewDidLoad]; _data = [[NSMutableData alloc]init]; //判断token有没有过期 NSDate *endDate = [USER_D objectForKey:@"endDate"]; NSDate *nowDate = [NSDate date]; //比较现在时间和过去时间 ascend ,上升 if ([nowDate compare:endDate] == NSOrderedAscending) { //token没有过期 FriendsListVC *vc = [[FriendsListVC alloc]init]; [self.navigationController pushViewController:vc animated:YES]; [vc release]; } else { //token过期 }}- (IBAction)login:(UIButton *)sender{ NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/st/s"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSString *string = [NSString stringWithFormat:@"command=ST_L&name=%@&psw=%@",_nameField.text,_pwdField.text]; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; [request setHTTPMethod:@"POST"]; NSURLConnection *connetion = [NSURLConnection connectionWithRequest:request delegate:self]; [connetion start];}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"失败:%@",error);}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ [_data setData:[NSData data]];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [_data appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *string = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string); NSLog(@"%@",_data); //NSJSONSerialization JSON解析器 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil]; if ([[dic objectForKey:@"result"]integerValue] == 1) { //登录成功 //access_token ⽤用户⾝身份验证通过返回的唯⼀一标⽰示⽤用户⾝身份的字段 //将token保存起来,用于之后调用需要登录验证的接口 [USER_D setObject:[dic objectForKey:@"access_token" ] forKey:@"token"]; //计算过期时间 NSDate *nowDate = [NSDate date]; //dateByAddingTimeInterval 计算一个时间点加上一段时间后的时间点 NSDate *endDate = [nowDate dateByAddingTimeInterval:[[dic objectForKey:@"time"] integerValue]]; //存储过期时间 [USER_D setObject:endDate forKey:@"endDate"]; [USER_D synchronize]; FriendsListVC *vc = [[FriendsListVC alloc]init]; [self.navigationController pushViewController:vc animated:YES]; [vc release]; } else { SHOW_ALERT([dic objectForKey:@"error"]); } [string release];}- (IBAction)regist:(UIButton *)sender{ RegistVC *vc = [[RegistVC alloc]init]; [self.navigationController pushViewController:vc animated:YES]; [vc release];}
可以和XML格式对比一下
XML数据格式原生解析器
7 0
- json数据格式原生解析器
- XML数据格式原生解析器
- android 解析json数据格式
- android 解析json数据格式
- Android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- Android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- android 解析json数据格式
- Android学习心得(19) --- short、byte、Hex、int、ULeb128类型转化Java源码
- Flex弹性盒模型初尝试
- Core Animation - 核心动画
- Android Java设计模式
- 树莓派上使用摄像头
- json数据格式原生解析器
- Direct3D基础概念和模型整理
- 注解学习
- AlexNet简单理解&&CNN初步了解
- Merge Sorted Array
- 2016.07.03砸题选讲(Worldwide_D)小结
- LeakCanary源码分析第一讲
- C++面试题(四)——智能指针的原理和实现
- windows下搭建Ruby(基于eclipse的环境)开发环境