gpt4 book ai didi

ios - 如何在 Json 请求中的参数中传递一个 ID 数组

转载 作者:行者123 更新时间:2023-11-29 01:32:10 25 4
gpt4 key购买 nike

我有一个 JSON 请求和一些参数...我需要在这些参数之一中传递一个数组...如何?

这是我当前的请求...我有 3 个参数:纬度、经度和数组:数组必须是 ids (int) 数组

数组:NSArray arrayIds = [NSArrayWithObjects:@"1", @"2", @"3", nil];

  -(void) listarAtracoesBusca
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
self.listaAtracoes = [[NSMutableArray alloc]init];
self.indiceAtracaoAtual = 0;
NSString *urlStr = @"";
float latitude = 0.0;
float longitude = 0.0;

if(appDelegate.latitudeAtual)
latitude = appDelegate.latitudeAtual;
if(appDelegate.longitudeAtual)
longitude = appDelegate.longitudeAtual;

[self validarCamposBusca];

urlStr = [NSString stringWithFormat:@"https://host:3000/api/ params?latitude=%f&longitude=%f&array=", latitude, longitude, arrayIds];


NSURL *URL = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setValue:@"haadushdiuashud" forHTTPHeaderField:@"X-User-Authorization"];
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", @"user", @"pass"];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)];

[request setValue:authValue forHTTPHeaderField:@"Authorization"];
request.HTTPMethod = @"GET";
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

//Do Stuff

if (data.length > 0 && error == nil)
{
}
else {
NSLog(@"erro: %@", error.description);
}
}];
}

最佳答案

如果你正在尝试实现查询 url 之类的东西,那么你可以使用 NSURLQueryItem,它将代表 URL 查询部分的单个键/值对。

NSURLComponents *components = [NSURLComponents componentsWithString:@"http://stackoverflow.com"];
NSURLQueryItem *search = [NSURLQueryItem queryItemWithName:@"q" value:@"ios"];
NSURLQueryItem *count = [NSURLQueryItem queryItemWithName:@"count" value:@"10"];
components.queryItems = @[ search, count ];
NSURL *url = components.URL; // http://stackoverflow.com?q=ios&count=10

否则,您需要使用 NSJSONSerialization 将 JSON 作为请求的主体传递。引用here了解更多详情。

关于ios - 如何在 Json 请求中的参数中传递一个 ID 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33343665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com