当前位置: 首页 > 面试题库 >

使用AFNetworking发送嵌套的JSON

端木乐语
2023-03-14
问题内容

要将注册数据发送到服务器,我使用的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 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x94b3c30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}


问题答案:

您的要求很好。Error Domain=NSCocoaErrorDomain Code=3840由于服务器使用无效的JSON对象进行响应,因此将返回错误。NSLog
operation.responseString看看发回了什么。



 类似资料:
  • 问题内容: 我正在尝试使用HTTPie进行分析以发送一些嵌套的JSON对象,但我找不到方法。很清楚如何发送JSON对象,而不发送嵌套对象,例如 {“ user”:{“ name”:“ john”“ age”:10}} 问题答案: 您可以通过传递整个JSON: 或者使用以下命令将原始JSON指定为值:

  • 使用Postman,我可以用表单数据在请求中附加文件,我也可以用原始格式发送嵌套的JSON对象,但是我如何做到这两个:用文件发送嵌套结构的数据,例如:

  • 问题内容: 我需要使用ajax和FormData发送一些数据,因为我想发送文件和其他一些参数。我通常发送数据的方式是这样的: 如果我不使用FormData(),就没有问题,但是当使用FormData()时,只有Lvl1上的数据可以,但是嵌套的任何内容都显示为这样的字符串 如果我使用FormData()对Lvl1-3中的数据进行编码,而不是得到 如何在Lvl1-3上获取数组而不是字符串? 注意:如果

  • 我正在使用ASP。NET Web API:http://xyzdomain.com:16845/api/returns/returns 如何使用PostmanChrome扩展向终结点发送POST请求,给定是一个集合:

  • 问题内容: 我有一个Web脚本,该脚本接受JSON字符串作为通过HTTP POST请求的输入。我碰到了几个相同的AFNetworking 1.x示例,有人可以指点我还是举个AFNetworking 2.0示例向以JSON格式输入的Web脚本发出HTTP POST请求? 谢谢 问题答案: 搜索文档并尝试一些代码后,我以以下示例为例 同样不要忘记在服务器脚本中将响应头类型设置为Application

  • 我有一个很长的JSON正文,这是一个数组。我需要在POST方法的正文中发送这个。我使用放心。怎么做?