我不能用 JSONHTTPClient 发送参数数组吗?
Can't I send an array in params with JSONHTTPClient?
我正在尝试发送一些参数:一个 NSString 和一个 NSarray。
NSArray
打印时的格式是这样的:
(54,
55)
但是当我将它添加到 JSONHTTPClient 的参数中时,我得到了这个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request parameters can be
only of NSString or NSNumber classes.
我的代码是:
[JSONHTTPClient postJSONFromURLWithString:uploadUrl
params:@{@"idKey":@"689769", @"idFree":myArray}
completion:^(NSDictionary *json, JSONModelError *err)
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"complet");
});
}];
如何将数组发送到参数中?
如果库不支持,那么你可能需要自己序列化它。您可以为此使用 NSJSONSerialization Class:
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 所以它应该很高兴!
我正在尝试发送一些参数:一个 NSString 和一个 NSarray。
NSArray
打印时的格式是这样的:
(54,
55)
但是当我将它添加到 JSONHTTPClient 的参数中时,我得到了这个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request parameters can be only of NSString or NSNumber classes.
我的代码是:
[JSONHTTPClient postJSONFromURLWithString:uploadUrl
params:@{@"idKey":@"689769", @"idFree":myArray}
completion:^(NSDictionary *json, JSONModelError *err)
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"complet");
});
}];
如何将数组发送到参数中?
如果库不支持,那么你可能需要自己序列化它。您可以为此使用 NSJSONSerialization Class: 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 所以它应该很高兴!