簡單地說就是我做的一個登錄驗證從網站返回的數據,我現在要把數據解析出來但是我不會呀。.h文件為#import
#import "JSONKit.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
@end
.m文件為
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
[textField resignFirstResponder];
return YES;
}
}
(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"];
你先試試吧,不行的話你再找我。