我需要将下面的字符串解析为 iOS 中的 POST 方法
"jsonData":{"empId":"cxvd","password":"sfsd"}
但是我收到的错误是
回复:Tomcat 错误
HTTP 状态 400 - 所需的字符串参数“jsonData”不存在
//------ Method I have used to Parse is ---------- //
+(void) requestToServerForLogin:(NSString*)userName andPassward: (NSString*)password onCompletion:(RequestCompletionHandler) handler
{
NSString *url = [Ip stringByAppendingString:@"login"];
NSString *jsonString = [NSString stringWithFormat:@"\"jsonData\":{\"empId\":\"%@\",\"password\":\"%@\"}",
userName,
password ];
NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:nsurl];
[urlRequest setTimeoutInterval:60.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json"
forHTTPHeaderField:@"Content-type"];
NSString *body = jsonString1;
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"urlRequest :%@",[body dataUsingEncoding:NSUTF8StringEncoding]);
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data1, NSError *error)
{
NSString *res = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
if(handler) handler(res,error);
}];
}
提前致谢
最佳答案
代码太多了。 substringToIndex
和 substringFromIndex
是错误的,不应该出现在代码中。
使用字典的文字语法:
NSDictionary *jsonDict = @{@"jsonData":@{@"password":password, @"empId":userName}};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];
关于objective-c - JSON解析中如何将Http请求中的json数据发送到POST方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28939890/