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

iphone – 如何在JSON中发布数组值,第1张

概述我想在 JSON中发布一个数组值. 以下是我的代码: -(void)getConnection { NSArray *comment=[NSArray arrayWithObjects:@"aaa",@"bbb",@"ccc",@"hello,yes,tell", nil]; NSURL *aurl=[NSURL URLWithString:@"http://sajalaya. 我想在 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中发布数组值全部内容,希望文章能够帮你解决iphone – 如何在JSON中发布数组值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1079602.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存