weixin_30260663
苏打熊
采纳率64.3%
2015-08-10 13:55 阅读 1.7k

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    ruzhuxiaogu ruzhuxiaogu 2015-08-10 15:22

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

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

    点赞 评论 复制链接分享
  • weixin_30260663 苏打熊 2015-08-10 14:09

    大神怎么还没来~迫不及待的等大神解决。

    点赞 评论 复制链接分享
  • ruzhuxiaogu ruzhuxiaogu 2015-08-11 02:28

    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData: received options:NSJSONReadingMutableLeaves error:nil];

    recieved 为你接收到的NSData类型。

    通过dict[@"ErrorCode"],dict[@"IsCompleted"]等来提取;

    我发现 键 result对应好像还是一个JSON数据的字符串,所以要再次解析出来**(我上次以为Result对应的是json,你的程序对应的是JSON字符串,要转化为JSON)**

    NSString *result=dict[@"Result"];

    NSData *jsonData = [result dataUsingEncoding:NSUTF8StringEncoding];

    NSError *err;

    NSDictionary *res = [NSJSONSerialization JSONObjectWithData: jsonData options:NSJSONReadingMutableContainers error:&err];

    if(err) {

    NSLog(@"json解析失败:%@",err);

    }

    NSString *company_code =res[@"CompanyCode"];
    NSString *company_name=res[@"CompanyName"];

    点赞 评论 复制链接分享
  • ruzhuxiaogu ruzhuxiaogu 2015-08-11 03:38

    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData: received options:NSJSONReadingMutableLeaves error:nil];

    recieved 为你接收到的NSData类型。

    通过dict[@"ErrorCode"],dict[@"IsCompleted"]等来提取;

    我发现 键 result对应好像还是一个JSON数据的字符串,所以要再次解析出来**(我上次以为Result对应的是json,你的程序对应的是JSON字符串,要转化为JSON)**

    NSString *result=dict[@"Result"];

    NSData *jsonData = [result dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *res = [NSJSONSerialization JSONObjectWithData: jsonData options:NSJSONReadingMutableLeaves error:nil];

    NSString *company_code =res[@"CompanyCode"];
    NSString *company_name=res[@"CompanyName"];

    不好意思,是我大意了,你试试这样,不行的话再call我。

    点赞 评论 复制链接分享

相关推荐