开发者社区> 问答> 正文

josn数据的解析呀呀呀。

简单地说就是我做的一个登录验证从网站返回的数据,我现在要把数据解析出来但是我不会呀。.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文件为

import "ViewController.h"

@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&parameter=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":""}"}大神告诉我如何解析并提取键值呀。

展开
收起
杨冬芳 2016-07-05 17:01:28 2059 0
1 条回答
写回答
取消 提交回答
  • IT从业

    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"];

    你先试试吧,不行的话你再找我。

    2019-07-17 19:51:40
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像