如何将ID转换为数组?
我有一个与服务器对话的苹果应用程序。
我遇到的问题是应用程序以id的形式返回数据,但是我需要将其转换为数组,因为实际返回的数据如下所示。
[["30","2",1],["15","67",1],["30","4",1]]
它实际上是mysql服务器的输出
实际的应用程序代码如下所示
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"blah blah"]];
NSURL_Layer * connection = [[NSURL_Layer alloc]initWithRequest:request];
[connection setCompletitionBlock:^(id obj, NSError *err) {
if (!err) {
//Need to convert the id to nsarray here, dunno how
} else {
//There was an error
}
}];
[connection start];
NSURL.h
-(id)initWithRequest:(NSURLRequest *)req;
@property (nonatomic,copy)NSURLConnection * internalConnection;
@property (nonatomic,copy)NSURLRequest *request;
@property (nonatomic,copy)void (^completitionBlock) (id obj, NSError * err);
-(void)start;
NSURL.m
-(id)initWithRequest:(NSURLRequest *)req {
self = [super init];
if (self) {
[self setRequest:req];
}
return self;
}
-(void)start {
container = [[NSMutableData alloc]init];
internalConnection = [[NSURLConnection alloc]initWithRequest:[self request] delegate:self startImmediately:YES];
if(!sharedConnectionList)
sharedConnectionList = [[NSMutableArray alloc] init];
[sharedConnectionList addObject:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[container appendData:data];
}
//If finish, return the data and the error nil
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
if([self completitionBlock])
[self completitionBlock](container,nil);
[sharedConnectionList removeObject:self];
}
//If fail, return nil and an error
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if([self completitionBlock])
[self completitionBlock](nil,error);
[sharedConnectionList removeObject:self];
}
更新:
我已经添加了
NSURL_Layer * connection = [[NSURL_Layer alloc]initWithRequest:request];
[connection setCompletitionBlock:^(id obj, NSError *err) {
if (!err) {
NSError* error;
NSArray* array = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingAllowFragments error:&error];
} else {
//There was an error
}
}];
[connection start];
但返回错误
error NSError * domain: @"NSCocoaErrorDomain" - code: 3840
_userInfo NSDictionary * 1个键/值对[0](空)@“ NSDebugDescription”:@“字符0周围的值无效。”
更新:我把
NSLog(@"Data as string:%@", [[NSString alloc]initWithData:obj encoding:NSUTF8StringEncoding]);
这给了我一个奇怪的反馈。结果,我在下面查看了我的url请求的完整代码。
NSString *post = [NSString stringWithFormat:@"unique_id=%@&unique_password=%@",ServerUniqueID,ServerPassword];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"blah blah"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURL_Layer * connection = [[NSURL_Layer alloc]initWithRequest:request];
[connection setCompletitionBlock:^(id obj, NSError *err) {
if (!err)
{
NSError* error;
NSArray* array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithData: obj] options:NSJSONReadingAllowFragments error:&error];
NSLog(@"Data as string:%@", [[NSString alloc]initWithData:obj encoding:NSUTF8StringEncoding]);
int temp = array.count;
}
else
{
//There was an error
}
}];
[connection start];
如果我删除
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
有用
如果它不这样做,那么我有一个全新的问题要研究。
您将字节保存到容器变量中,例如id实际上是NSData(注意id只是一个“通配符指针”,表示任何objC对象)
因此您的ID是NSData,从您显示的结果来看,它似乎是3个JSON数组...但没有真正的JSON ...([[“ 30”,“ 2”,1] [“ 15”,“ 67”,1] [“ 30”,“ 4”,1]无效)
使服务器向您发送JSON,然后您可以使用NSJSONSerialization解析为字典/数组,
或者编写自定义分隔符以转换数据
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句