gpt4 book ai didi

iphone - 如何在 JSON 中发布数组值

转载 作者:可可西里 更新时间:2023-11-01 05:56:47 24 4
gpt4 key购买 nike

我想在 JSON 中发布一个数组值。

下面是我的代码:

-(void)getConnection {

NSArray *comment=[NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",@"hello,yes,tell", nil];

NSURL *aurl=[NSURL URLWithString:@"http://sajalaya.com/taskblazer/staffend/form/iphonearraytest.php"];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:aurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPMethod:@"POST"];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:comment options:NSJSONWritingPrettyPrinted error:nil];
NSString *new = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];

// NSString *new = [comment JSONString];

// NSArray *new=[comment jsonvalue];

NSString *postString=[NSString stringWithFormat:@"tag=&comment=%@&total=%@",new,@"4"];

NSLog(@"this is post string%@",postString);
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

[NSURLConnection connectionWithRequest:request delegate:self];
}

最佳答案

我们不知道您的问题,但我的回答很简短。你应该为此使用很棒的开源库,它是:AFNetworking ,并像这样请求:

_httpClient = [[AFHTTPClient alloc] initWithBaseURL:[[NSURL alloc] initWithString:@"http://sajalaya.com"]];

[_httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:comment options:NSJSONWritingPrettyPrinted error:nil];
NSString *new = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
new, @"comment",
@4, @"total,
nil];

NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"POST"
path:@"/taskblazer/staffend/form/iphonearraytest.php"
parameters:params];
request.timeoutInterval = 8;

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure
}];

关于iphone - 如何在 JSON 中发布数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565057/

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