程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-josn數據的解析呀呀呀。

ios-josn數據的解析呀呀呀。

編輯:編程綜合問答
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\":\"\"}"}大神告訴我如何解析並提取鍵值呀。

最佳回答:


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
weixin_30260663
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved