Post Json 格式的请求 iOS
Post Request in Json format iOS
我是 iOS 的新手,我必须以 json
格式发送 post 请求。像,
{"user":{"email":"abc@gmail.com"}}
我的编码就像,
NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
[postDict setValue:@"test@gmail.com" forKey:@"email"];
// [postDict setValue:text_password.text forKey:@"password"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
我正在获取输出
{"email":"test@gmail.com"}
请告诉我如何添加{"user":
谢谢。
1) Post 使用默认函数的数据:-
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"test@gmail.com" forKey:@"email"];
NSString *post =[dict JSONRepresentation];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:”YourURL”]]];
// [request setTimeoutInterval:30.0f];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
2) Post 使用 AFNetworking 库的数据 :-
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"test@gmail.com" forKey:@"email"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@“Your URL”]]];
[request setHTTPMethod:@"POST"];
NSString *post =[dict JSONRepresentation];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];
//call start on your request operation
[operation start];
我建议以更动态的方式增强你的思维,因为这很容易做到,但对于你的问题,你可以通过这样的代码来实现,
NSMutableDictionary *postDict =[[NSMutableDictionary alloc] init];
NSMutableDictionary *dicEmail =[[NSMutableDictionary alloc] init];
[dicEmail setObject:@"abc@gmail.com" forKey:@"email"];
[postDict setObject:dicEmail forKey:@"user"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
享受编码吧!!
If you want - {"user":{"email":"abc@gmail.com"}}
那么你需要两本字典。
内部词典的第一个 - {"email" : "abc@gmail.com"}。并且
第二个用于外部字典 - {"user" :{....}}
所以首先创建内部 dictionary.Such as-
NSMutableDictionary *data=[[NSMutableDictionary alloc]init];
[data setValue:@"abc@gmail.com" forKey:@"email"];
现在将外部 dictionary.Such 创建为-
NSMutableDictionary *userInfo=[[NSMutableDictionary alloc]init];
[userInfo setValue:data forKey:@"user"];
现在创建 JSON 对象-
NSData *JSONData=[[NSData alloc] init];
if([NSJSONSerialization isValidJSONObject:userInfo])
{
JSONData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:nil];
}
NSString *jsonString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
NSLog(@"JSON Output: %@", jsonString);
输出将是-
{"user":{"email":"abc@gmail.com"}}
希望对您有所帮助。
再制作一个字典并将第一个字典传递给第二个你得到你的格式
NSMutableDictionary *firstDic=[[NSMutableDictionary alloc]init];
[data setValue:@"abc@gmail.com" forKey:@"email"]; NSMutableDictionary
*secondDic=[[NSMutableDictionary alloc]init];
[userInfo setValue:firstDic forKey:@"user"];
希望对您有所帮助
我是 iOS 的新手,我必须以 json
格式发送 post 请求。像,
{"user":{"email":"abc@gmail.com"}}
我的编码就像,
NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
[postDict setValue:@"test@gmail.com" forKey:@"email"];
// [postDict setValue:text_password.text forKey:@"password"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
我正在获取输出
{"email":"test@gmail.com"}
请告诉我如何添加{"user":
谢谢。
1) Post 使用默认函数的数据:-
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"test@gmail.com" forKey:@"email"];
NSString *post =[dict JSONRepresentation];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:”YourURL”]]];
// [request setTimeoutInterval:30.0f];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
2) Post 使用 AFNetworking 库的数据 :-
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"test@gmail.com" forKey:@"email"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@“Your URL”]]];
[request setHTTPMethod:@"POST"];
NSString *post =[dict JSONRepresentation];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%ld",(long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
//Add your request object to an AFHTTPRequestOperation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];
//call start on your request operation
[operation start];
我建议以更动态的方式增强你的思维,因为这很容易做到,但对于你的问题,你可以通过这样的代码来实现,
NSMutableDictionary *postDict =[[NSMutableDictionary alloc] init];
NSMutableDictionary *dicEmail =[[NSMutableDictionary alloc] init];
[dicEmail setObject:@"abc@gmail.com" forKey:@"email"];
[postDict setObject:dicEmail forKey:@"user"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil];
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
享受编码吧!!
If you want - {"user":{"email":"abc@gmail.com"}}
那么你需要两本字典。
内部词典的第一个 - {"email" : "abc@gmail.com"}。并且
第二个用于外部字典 - {"user" :{....}}
所以首先创建内部 dictionary.Such as-
NSMutableDictionary *data=[[NSMutableDictionary alloc]init];
[data setValue:@"abc@gmail.com" forKey:@"email"];
现在将外部 dictionary.Such 创建为-
NSMutableDictionary *userInfo=[[NSMutableDictionary alloc]init];
[userInfo setValue:data forKey:@"user"];
现在创建 JSON 对象-
NSData *JSONData=[[NSData alloc] init];
if([NSJSONSerialization isValidJSONObject:userInfo])
{
JSONData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:nil];
}
NSString *jsonString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
NSLog(@"JSON Output: %@", jsonString);
输出将是- {"user":{"email":"abc@gmail.com"}}
希望对您有所帮助。
再制作一个字典并将第一个字典传递给第二个你得到你的格式
NSMutableDictionary *firstDic=[[NSMutableDictionary alloc]init];
[data setValue:@"abc@gmail.com" forKey:@"email"]; NSMutableDictionary
*secondDic=[[NSMutableDictionary alloc]init];
[userInfo setValue:firstDic forKey:@"user"];
希望对您有所帮助