[英]Objective-C How to send data to Http Post request with nested dictionary in header
我学习Objective-C已有几个月了。 那时,我已经可以执行基本的发帖请求,但是今天我看到了包含嵌套字典的发帖请求的标题。 我不确定如何将数据传递到这样的API。
我的问题是如何将数据发布到此API。 如果您能指向相关阅读材料和教程,例如复杂的API请求或给出示例,将不胜感激。
API标头如下所示:
{ "header": { "id": "2312b24c-5b83-46f0-8670-5edc2b1d1672", "parentId": "3gyy-w325e-bebd-ddsfsdf", "countryCode": "IRELAND", "timestamp": "2017-10-13T08:39:14.638Z" }, "myinfo": { "User": "intro", "contactPerson": "Newbee", "contact": "00000000", "notes": "hey guys i am new" } }
这是我的代码,我需要多个词典的帮助。
__block NSMutableDictionary *resultsDictionary;
SMAMobileAPI *api = [SMAMobileAPI sharedSMAMobileAPI];
NSMutableDictionary *bodyParams = [[NSMutableDictionary alloc]init];
NSMutableDictionary *header = [[NSMutableDictionary alloc]init];
[header setValue:@"2312b23c-5b83-46f0-8670-5edc2b1d1672" forKey:@"id"];
[header setValue:@"2e3a015e-bebd-ddsfsdf" forKey:@"parentId"];
[header setValue:@“IRELAND" forKey:@"countryCode"];
[header setValue:@"2017-10-13T08:39:14.638Z" forKey:@"timeStamp"];
NSMutableDictionary *myinfo = [[NSMutableDictionary alloc]init];
[Case setValue:@“intro" forKey:@“User"];
[Case setValue:@“newbie" forKey:@"contactPerson"];
[Case setValue:@"+254 724474140" forKey:@"contactPersonNo"];
[Case setValue:@"iOS case" forKey:@"notes"];
[bodyParams setValue:header forKey:@"header"];
[bodyParams setValue:myinfo forKey:@“myinfo"];
if ([NSJSONSerialization isValidJSONObject:bodyParams]) {//validate it
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:bodyParams options:NSJSONWritingPrettyPrinted error: &error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:@“my URL"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];//use POST
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:jsonData];//set data
__block NSError *error1 = nil;
//use async way to connect network
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse* response,NSData* data,NSError* error)
{
if ([data length]>0 && error == nil) {
resultsDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(@"resultsDictionary is %@",resultsDictionary);
[self hideLoading];
} else if ([data length]==0 && error ==nil) {
NSLog(@" download data is null");
[self hideLoading];
} else if( error!=nil) {
NSLog(@" error is %@",error);
[self hideLoading];
}
}];
}
}
似乎将NSDictionary转换为JSON是您的实际问题。 请参阅文档https://developer.apple.com/documentation/foundation/jsonserialization
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:yourDictionary
options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.