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)

}


}

你可能感兴趣的:(ios开发小点积累)