使用jsonkit封装字典为json数据
/************第一种方法:使用jsonkit封装字典为json数据************/
NSDictionary *dataDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
@"100110004777",@"no",
@"123321",@"pwd",
@"1111",@"check_code",
nil];
NSString *tempJsonData=[dataDictionary JSONString];
NSData *jsonData = [tempJsonData dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *JsonDataOK= [NSMutableData dataWithData:jsonData];
NSURL *url1 = [NSURL URLWithString:@"http://172.17.49.200:8341/app/100001.ha"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url1];
[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request setPostBody:JsonDataOK];
[request setDelegate:self];
[request startSynchronous];
/************第二种方法:使用原生方法封装字典为json数据************/
// NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:
// @"100110004777",@"no",
// @"123321",@"pwd",
// @"4620",@"check_code",
// nil];
// if ([NSJSONSerialization isValidJSONObject:user])
// {
// NSError *error;
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
// NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
// //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);
//
// NSURL *url = [NSURL URLWithString:@"http://172.17.49.200:8341/app/100001.ha"];
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
// [request addRequestHeader:@"Accept" value:@"application/json"];
// [request setRequestMethod:@"POST"];
// [request setDelegate:self];
// [request setPostBody:tempJsonData];
// [request startSynchronous];
// NSError *error1 = [request error];
// if (!error1) {
// NSString *response = [request responseString];
// NSLog(@"Test:%@",response);
// }
// }