【问题标题】:How to post an array value in JSON如何在 JSON 中发布数组值
【发布时间】:2012-11-26 12:41:29
【问题描述】:

我想在 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];       
}

【问题讨论】:

  • 问题是什么?错误是什么?

标签: iphone objective-c ios ipad ios5


【解决方案1】:

我们不知道您的问题,但我的回答很简短。您应该为此使用出色的开源库,即: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
}];

【讨论】:

  • 但我正在使用 json 框架......让我们举个例子意味着我必须发送一个数组值并且我正在发送但在后端他没有得到数组......他正在就像一个字符串......我被困在这种情况下......请任何人给我最好的选择
【解决方案2】:

请使用以下可变数组操作componentsJoinedByString

例如

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

NSString* strCommentsJoin = [commennts componentsJoinedByString:@","]; // Please use your separator 

【讨论】:

  • 我想将 json 数组发送到 php 链接,但在 php 端它变得像字符串,但我必须发送数组,他应该得到它像数组形式,,,,,,谢谢你回复
  • PHP/.net/Java 也有同样的功能将字符串元素分离到数组中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 2019-12-06
  • 2018-11-10
  • 2020-06-26
  • 2021-02-15
  • 2018-09-27
相关资源
最近更新 更多