背景:继续破解打卡,用IOS开发出一个APP.单个helloWorldOK了,和后端互动成功,现在就要将解析出的Json的结果放在页面上。同时期望按钮点击后,能改变页面上某些Label的值。
环境:Mac OS 10.11.5+Xcode7.3.1
1、IOS自从5以后就自带了解析了Json的功能,有一个类叫NSJSONSerialization,参考教程
如果自己懒得写后台Json的话,可以用一些公共的Json来练习。
2、按钮点击的事件,可以通过IBAction来绑定。具体操作就是在Story面板上,选择某个按钮,右键后会出现一堆事件列表,选择一个,点加号,然后拖到ViewController.m中,这样就会建立一个链接了。
3、关于事件点击后,如何控制Label的值。这个要用Outlet来解决。思路和2非常像,不过在拖动之前,需要先在ViewController.h和ViewController.m中分别新建Property和thesize.下面的动作就和2差不多了。在某个按钮上右击,找到.h中的Outlet,拖动即可。
4、控制页面跳转教程(仍未测试)
总结:IOS基础非常不扎实,导致好多地方走了非常多的弯路,仍然没走过去。
主要体现在:Json出来的数据判断报错或者无效。给Story上的控件赋值报错
下面附上我Controller的源码,写的较挫,回头还要再优化。
// // ViewController.m // dakaDemo // // Created by gemmy on 16/7/24. // Copyright © 2016年 shundepg. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize labelDate; @synthesize initBtn; @synthesize amBtn; @synthesize pmBtn; @synthesize labelAmId; @synthesize labelPmId; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)initClick:(id)sender { NSError *error; //1.确定请求路径 //NSURL *url = [NSURL URLWithString:@"http://apistore.baidu.com/microservice/weather?cityid=101010100"]; NSURL *url = [NSURL URLWithString:@"http://115.182.83.55:8003/mobile"]; //2.创建可变的请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.修改请求方法为POST(大写) request.HTTPMethod = @"POST"; //3.1 人为构造服务器需要的json格式 NSDictionary * dic= @{@"methodName":@"getMyAttendance",@"orderNo":@"1463358870034",@"version":@"1.1.0",@"needReLogin":@"false",@"osType":@"IOS",@"deviceToken":@"",@"IOSUuid":@"201605160834302541341878187",@"mac":@"201605160834302541341878187",@"token":@"1b38dfa19a056fdb101543c5a6f6cdde",@"ip":@"",@"email":@"gogogo@xxx.com",@"userName":@"张大吉",@"companyLogoUrl":@"http://www.xxxtd.com/pc/images/icon_logo.png",@"phone":@"18812345678"}; NSLog(@"DIC = %@",dic); NSData * data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error]; // 把一个json格式的data,解析为OC中的NSString类型对象 // 无特殊意义,一般用来查看JSON文本封装成 数组 还是 对象(OC中为字典) NSString * jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"-----------------------------%@",jsonString); NSString *dataStr=[NSString stringWithFormat:@"requestJson=%@",jsonString]; //4.设置请求体 request.HTTPBody = [dataStr dataUsingEncoding:NSUTF8StringEncoding]; //设置请求头信息 [request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"]; //设置请求超时 request.timeoutInterval = 60; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 //这里是完整的Json串 NSDictionary *fullDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSArray *objectArray = [fullDic objectForKey:@"object"]; NSLog(@"objectArray:%@",objectArray); //遍历输出object里的list for(NSDictionary *aObject in objectArray){ NSArray *attendArray = [aObject objectForKey:@"list"]; NSInteger count=0; for(NSDictionary *aAttend in attendArray){ NSString *id = [aAttend objectForKey:@"id"]; NSString *isClock = [aAttend objectForKey:@"isClock"]; NSString *date = [aAttend objectForKey:@"date"]; if(count == 0){ //1.分别给控件设置状态 //1.1 日期设置好 // labelDate.text=date; //1.2 设置上班打卡状态 // if([isClock isEqualToString:@"1"]){ // [amBtn setTitle:@"已打卡" forState:UIControlStateNormal]; // }else if([isClock isEqualToString:@"0"]){ // [amBtn setTitle:@"未打卡" forState:UIControlStateNormal]; // } // [amBtn setTitle:isClock forState:UIControlStateNormal]; //1.3 设置id labelAmId.text=id; NSLog(@"id:%@",id); }else if (count ==1 ){ //2.分别给控件设置状态 //2.2 设置下班打卡状态 // if([isClock isEqualToString:@"1"]){ // [amBtn setTitle:@"已打卡" forState:UIControlStateNormal]; // }else if([isClock isEqualToString:@"0"]){ // [amBtn setTitle:@"未打卡" forState:UIControlStateNormal]; // } //1.3 设置id labelPmId.text=id; NSLog(@"id:%@",id); } count++; } } NSLog(@"attendency字典里面的内容为--》%@", fullDic ); //1.点击此按钮后,将未打卡的状态全部置成已打卡 //1.1 设置日期 // labelDate.text=[weatherInfo objectForKey:@"city"];//1 //1.2 设置上班打卡状态 // [amBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; //1.3 设置下班打卡状态 // [pmBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; } - (IBAction)amBtnClick:(id)sender { //1.确定请求路径 NSURL *url = [NSURL URLWithString:@"http://apistore.baidu.com/microservice/weather?cityid=101010100"]; //2.创建可变的请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.修改请求方法为POST(大写) request.HTTPMethod = @"POST"; //4.设置请求体 //request.HTTPBody = [@"username=dede&pwd=ede&type=JSON" dataUsingEncoding:NSUTF8StringEncoding]; //设置请求头信息 [request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"]; //设置请求超时 request.timeoutInterval = 10; NSError *error; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"retData"]; NSString *str = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic ); NSLog(@"%@", str ); //1.2 设置上班打卡状态 [amBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; NSLog(@"amBtn..."); } - (IBAction)pmBtnClick:(id)sender { NSError *error; //加载一个NSURL对象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apistore.baidu.com/microservice/weather?cityid=101010100"]]; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"retData"]; NSString *str = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]]; NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic ); NSLog(@"%@", str ); //1.3 设置下班打卡状态 [pmBtn setTitle:[weatherInfo objectForKey:@"city"] forState:UIControlStateNormal]; NSLog(@"amBtn..."); } @end
相关推荐
SuperObject 是开源的 Delphi 的 JSON 工具包,可生成 JSON 数据以及进行 JSON 解析。
iOS下解析json数据,自己亲自使用过,挺好用的。 使用JSONKit的解析方法:(需导入包:#import "JSONKit/JSONKit.h") - (void)printJson{ //如果json是“单层”的,即value都是字符串、数字,可以使用...
写了一个使用json官方工具包解析json的例子,共参考,可以去博客看
ios下json数据解析的TouchJson的库, 欢迎下载
iOS xml解析和json解析demo
1.简单的ios-json解析 2.在程序里的json串,解析为3个数组 3.涉及字典
ios TouchJson库解析json文件
本demo 是解析json数据 利用 苹果自带的解析技术解析json 也是json 解析数据 推荐的和优先考虑的
ios中解析json对象基类中需要用的代码
ios下三种解析json的库 sbjson TouchJson JSONKit 可直接使用
ios下json数据解析的JsonKit库
ios下json数据解析的SBJson 库
JSONKIT 为IOS解析最快的JSON解析工具类.
完美支持UNITY中JSON的解析和生成。IOS和Android 完全支持,比SimpleJSON更强大。c#版本
iOS开发,解析JSON数据的6种方法。可以直接运行
移动开发抓包工具和IOS JSON解析工具,希望对大家有用
iOS json解析出错 对于json格式,我们并不陌生,但是因为它处于不同的语言标准也不一致。分享一下最近项目出现的json问题: 1、编码问题,在不清楚服务端的编码格式的情况下,我直接使用: NSData *data = ...
ios下json数据解析的实例,完整代码,欢迎下载 如有疑问,请参考http://blog.csdn.net/abc13939746593/article/details/8764160
NULL 博文链接:https://ytwhw.iteye.com/blog/1749858
使用IOS5自带解析类NSJSONSerialization方法把数据转换为Json字符串以及解析Json字符串