Iphone 使用AFN网络发送嵌套JSON

Iphone 使用AFN网络发送嵌套JSON,iphone,ios,json,nsdictionary,afnetworking,Iphone,Ios,Json,Nsdictionary,Afnetworking,要将注册数据发送到服务器,我使用的JSON格式如下: {"regData": { "City":"Some City", "Country":"Some Country", "Email_Id":"abc@gmail.com", "MobileNumber":"+00xxxxxxxxxx", "UserName":"Name Of user" } } 这是我如何发送的 NSURL * url = [[NSURL alloc] initWithString:registerUrlS

要将注册数据发送到服务器,我使用的JSON格式如下:

{"regData": {
 "City":"Some City",
 "Country":"Some Country",
 "Email_Id":"abc@gmail.com",
 "MobileNumber":"+00xxxxxxxxxx",
 "UserName":"Name Of user"
 }
}
这是我如何发送的

 NSURL * url = [[NSURL alloc] initWithString:registerUrlString];
            AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
            httpClient.parameterEncoding = AFJSONParameterEncoding;
            [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
            NSDictionary * params = @{@"regData": @{
                                              @"City": self.cityField.text,
                                              @"Country": self.countryField.text,
                                              @"Email_Id": self.emailField.text,
                                              @"MobileNumber": self.numberField.text,
                                              @"UserName": self.userName.text,
                                              }
                                      };

            NSMutableURLRequest * request = [httpClient requestWithMethod:@"POST" path:registerUrlString parameters:params];
            AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                NSLog(@"Success: %@", JSON);

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            NSLog(@"Error: %@", [error debugDescription]);
            }];

            [operation start];
但不幸的是,我得到了这个错误:


Error Domain=NSCocoaErrorDomain code=3840“操作无法完成。(Cocoa错误3840)。“(JSON文本未以数组或对象开头,允许未设置片段的选项。)UserInfo=0x94b3c30{NSDebugDescription=JSON文本未以数组或对象开头,允许未设置片段的选项。}

尝试此操作以获取实际错误

NSLog(@"Error: %@", [error debugDescription]);
NSLog(@"Error: %@", [error localizedDescription]);

你的要求很好。返回错误
error Domain=nscococaerordomain code=3840
,因为服务器使用无效的JSON对象进行响应
NSLog
操作。responseString
查看返回的内容

我收到这个错误:error Domain=NSCocoaErrorDomain Code=3840“操作无法完成。(Cocoa错误3840)。“(JSON文本没有以数组或对象开头,允许未设置片段的选项。)UserInfo=0x94b3c30{NSDebugDescription=JSON文本没有以数组或对象开头,允许未设置片段的选项。}谢谢,马特!我已经解决了这个问题。问题正如你提到的。我从服务器获取字符串而不是json。@mattt,我将其子类化为,
AFHTTPSessionManager
在哪里可以找到
操作。responseString
?@Hemang您是否找到了与“Error Domain=nscocaerorDomain code=3840”相关的解决方案?不完全是这样,但您可以要求您的API开发人员记录这些问题,所以你可以在浏览器中查看它。也许这很关键,但我想对于我的案例来说,这起到了关键作用:)@AkhtarThanks非常感谢您的反馈@Hemang:)问题在于WEB API中没有发送正确的jsson字符串。我只是记录响应数据并得到错误。下面是代码:failure:^(AFHTTPRequestOperation*operation,NSError*error){NSLog(@“response data:%@”,operation.responseData);NSLog(@“response String:%@”,operation.responseString);NSLog(@“error:%@”,error);[self-errorResponseHandlingWithResponseDict:error.userInfo WithTag:tag];}您的方法不是泛型的。请检查正确的方法。不太容易出错且易于维护