苏打熊 2015-08-10 13:55 采纳率: 64.3%
浏览 1668
已采纳

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧