ios進行restful接口調用和json數據利用自帶NSJSONSerialization進行數據解析

NSError   *error=nil;

NSURL *url=[NSURL URLWithString:@"http://10.7.128.15:8089/gwk_alat/rest/MyPro/info"];

//這裏利用NSUrlconnection進行post請求get不在做案例

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];

request.HTTPMethod=@"POST";

NSString *str=[NSString stringWithFormat:@"loginname=%@",@"dhj"];

request.HTTPBody=[str dataUsingEncoding:NSUTF8StringEncoding];

[NSURLConnection sendAsynchronousRequest:request  queue:[NSOperationQueue alloc] init] completionHandler:^(NSURLresponse * response,NSData *data,NSError *connectionError){

if(connectionError==nil){

NSData *str=[NSURLConnection sendSynchronousRequet:request returningResponse:nil error:nil];

//解析str的json串

NSDictionary *root=[NSJSONSerialization JSONObjectWithData:str options:NSJSONReadingMutableLeaves error:nil];

NSString    *value=[root objectForKey:@"name"];

NSLog(@"%@",value)

}


}

發佈了31 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章