Objective-C如何将nsdata从Web服务转换为nsarray

我需要帮助

如何将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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将NSArray转换为NSData iOS Objective-C

Objective-C如何将NSURL转换为NSString?

如何将此Swift语法转换为Objective C?

我如何将其从Objective-C转换为Swift

如何将此Swift语法转换为Objective-C?

如何将Objective-C块转换为Swift闭包?

Objective-C如何将击键转换为ASCII字符代码?

如何将Swift类转换为UnsafePointer,例如Objective-C中的__bridge

如何将整个文件夹或程序包从Java转换为Objective-C?

如何将 HEXString (NSString) 转换为 ByteArray Objective-C

如何将Objective-C代码转换为Swift代码-错误处理,iOS

如何将iOS Objective-C类别接口代码转换为RubyMotion?

如何将Objective-C App委托转换为Swift?

如何将整数转换为Objective-C中的相应单词?

Objective-C-如何将文件路径转换为符合外壳程序的文件路径

如何将位操作代码从Java转换为Objective-C?

如何将UIImagePicker图像转换为字节数组Objective-C

如何将Objective-C`sortedArrayUsingSelector:NSSelectorFromString`转换为swift?

如何将字符串转换为字典 iOS Objective C

在Objective-C中将NSArray转换为NSString

将NSArray转换为在Objective-C或Swift中按频率排序的唯一值数组

如何在 iOS Objective C 中将 NSData 转换为 UInt8 数组?

将Java代码转换为Objective C

将 Swift 转换为 Objective C

将代码从 Swift 转换为 Objective C?

如何在Objective-C / iOS中将JavaScript数组转换为NSArray?

如何在Objective-C中将XML从Web服务解析为NSArray

如何将Objective-C工厂方法转换为Swift便捷初始化程序?

如何将使用GKAchievement的简单方法从Objective-C转换为C#?