简单地说就是我做的一个登录验证从网站返回的数据,我现在要把数据解析出来但是我不会呀。.h文件为#import
#import "JSONKit.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
(IBAction)signin:(id)sender;
@end
.m文件为
@interface ViewController ()
@end
@implementation ViewController
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
[textField resignFirstResponder];
return YES;
}
(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)signin:(id)sender {
NSString*username=self.username.text;
NSString*pwd=self.pwd.text;
NSString *str =[NSString stringWithFormat:@"ActionCode=0x2001¶meter=pwd###%@#####userNo###%@&LoginID=&LoginIp=",pwd,username];//设置参数
NSString *encodedValue = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*str2=[NSString stringWithFormat:@"http://14.17.84.128:8088/wcf/ActionService.svc/action/web/get/do?%@",encodedValue];
NSURL*url=[NSURL URLWithString:str2];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//NSURLRequest初始化方法第一个参数:请求访问路径,第二个参数:缓存协议,第三个参数:网络请求超时时间(秒)
//第三步,连接服务器
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str3 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
// NSLog(@"%@",str2);
NSLog(@"%@",str3);
}
@end接受到的数据为2015-08-10 21:36:37.038 百汇百通[626:6884] {"ErrorCode":0,"Exception":null,"IsCompleted":true,"Result":"{"CompanyCode":"100000","CompanyID":"1","CompanyName":"百汇百通位置服务平台","DataRights":"ALL","GprsPort":"","GpsDeviceNo":"","GpsDeviceType":"","LicensePlate":"","Privileges":"ALL","Result":"0","RoleID":"1","RoleName":"系统管理员","SimcardNo":"","UserName":"超级管理员","UserType":"1","VehicleID":""}"}大神告诉我如何解析并提取键值呀。
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData: received options:NSJSONReadingMutableLeaves error:nil];
recieved 为你接收到的NSData类型。
通过dict[@"ErrorCode"],dict[@"IsCompleted"]等来提取;
我发现 键 result对应好像还是一个JSON数据,所以要再次解析出来
NSDictionary *res=dict[@"Result"];
然后再取Result里的键值。比如:
NSString *company_code =res[@"CompanyCode"];
NSString *company_name=res[@"CompanyName"];
你先试试吧,不行的话你再找我。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。