使用键和值ios发布JSON数据

时间:2016-08-11 10:32:56

标签: ios objective-c

我发布了一个带有url及其参数的JSON数据。问题是,当我在Postman中发布它时,我获得“状态”:“成功”,“msg”:“你的会员表格提交成功完成”但当我在我的Xcode 7.2.1中发布它然后它给出了"status":"Failed","msg":"You Membership Form did not Submit SuccessFully"那么请建议我如何解决它。我的方式如下: -

NSError *error;
NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url111=[NSURL URLWithString:@"http://dnddemo.com/lifestyle/development/membership_life_api.php"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url111 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSDictionary *mapData=[[NSDictionary alloc]initWithObjectsAndKeys:@"_levelOfMembershipTableView",@"menu-291",@"_upgradeMembershipTableView",@"menu-565",@"_detailsTableView",@"menu-696",@"_txtFirstNameMembership.text",@"text-708",@"_txtLastNameMembership.text",@"text-274",@"_txtDOBMembership.text",@"text-443",@"_txtCompanyNameMembership.text",@"text-970",@"_txtJobTitleMembership.text",@"text-961",@"_txtAddressMembership.text",@"text-785",@"_txtCityMembership.text",@"text-482",@"_txtPostCodeMembership.text",@"text-760",@"_countryTableView",@"menu-19",@"_txtEmailMembership.text",@"text-577",@"_txtHomePhoneMembership.text",@"text-760",@"_txtMobilePhoneMembership.text",@"text-394",@"_txtWorkPhoneMembership.text",@"text-914",@"_contactByTableView",@"menu-373",@"_interestedInTableView",@"menu-527",@"_hearAboutUsTableView",@"menu-997", nil];
NSMutableArray *arr = [NSMutableArray arrayWithObjects:mapData,nil];
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error] encoding:NSUTF8StringEncoding];
NSDictionary *finalParams = @{@"field_name_value": createJSON};
[request setHTTPBody:[NSKeyedArchiver archivedDataWithRootObject:finalParams]];
NSURLSessionDataTask *postDataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) 
{
    NSString *string=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Result is=%@",string);
}];
[postDataTask resume];

我发送“field_name_value”作为密钥。我寄给邮递员的参数是: -

键 - field_name_value 价值 -

 [{ "field_value":"_levelOfMembershipTableView","field_name":"menu-291" }, { "field_value":"_upgradeMembershipTableView","field_name":"menu-565" } ,{ "field_value":"_detailsTableView","field_name":"menu-696" },{ "field_value":"_txtFirstNameMembership.text","field_name":"text-708" },{ "field_value":"_txtLastNameMembership.text","field_name":"text-274" },{ "field_value":"_txtDOBMembership.text","field_name":"text-443" },{ "field_value":"_txtCompanyNameMembership.text","field_name":"text-970" },{ "field_value":"_txtJobTitleMembership.text","field_name":"text-961" },{ "field_value":"_txtAddressMembership.text","field_name":"text-785" },{ "field_value":"_txtCityMembership.text","field_name":"text-482" },{ "field_value":"_txtPostCodeMembership.text","field_name":"text-760" },{ "field_value":"_countryTableView","field_name":"menu-19" },{ "field_value":"_txtEmailMembership.text","field_name":"text-577" },{"field_value":"_txtHomePhoneMembership.text","field_name":"text-760"},{"field_value":"_txtMobilePhoneMembership.text","field_name":"text-394"},{"field_value":"_txtWorkPhoneMembership.text","field_name":"text-914"},{"field_value":"_contactByTableView","field_name":"menu-373"},{ "field_value":"_interestedInTableView","field_name":"menu-527"},{ "field_value":"_hearAboutUsTableView","field_name":"menu-997"}]

1 个答案:

答案 0 :(得分:0)

您的代码正在创建字典,将其转换为JSON数据,然后将JSON数据转换为字符串,将JSON数据中的字符串放入另一个字典,然后将NSKeyedArchiver的输出添加为HTTP身体。

这些步骤的四分之三是不必要和错误的。只需将您的字典转换为JSON NSData(使用NSJSONSerialization)并将其作为HTTP正文附加到您的请求中。