我不能用JSONHTTPClient在params中发送一个数组吗?

时间:2016-09-03 15:47:38

标签: ios objective-c json nsarray jsonhttpclient

我尝试发送一些参数:NSString和NSarray。

我打印时NSArray有这种格式:

(54,

55)

但是当我把它添加到JSONHTTPClient中的params时,我得到了这个错误:

  

***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'请求参数可以是   仅限NSString或NSNumber类。

我的代码是:

[JSONHTTPClient postJSONFromURLWithString:uploadUrl 
 params:@{@"idKey":@"689769", @"idFree":myArray}
                               completion:^(NSDictionary *json, JSONModelError *err)

 dispatch_async(dispatch_get_main_queue(), ^{

 NSLog(@"complet");
 });
}];

如何将数组发送到params?

1 个答案:

答案 0 :(得分:1)

如果库不支持它,那么您可能需要自己序列化它。您可以使用NSJSONSerialization类: https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/dataWithJSONObject:options:error

NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:myArray options:kNilOptions error:&error];
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

现在你有myString而不是myArray所以它应该很开心!