【问题标题】:ASIHTTPRequest convert response data to NSArrayASIHTTPRequest 将响应数据转换为 NSArray
【发布时间】:2013-02-14 13:27:24
【问题描述】:

我正在使用 ASIHTTPRequest 从服务器获取 JSON 响应。我想将此响应转换为 NSArray。我的代码:

    NSURL *url = [NSURL URLWithString:@"http://chirkov.net.ua/iosnettest/request.php?act=showdata&query=2;0;3;0;ASC"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) { 
    NSData *responseData = [[NSMutableData alloc] initWithData:[request responseData]];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:responseData];
    NSLog(@"array = %@",array);
}

但出现异常Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0xffffffef, 0xffffffbb, 0xffffffbf, 0x5b, 0x7b, 0x22, 0x69, 0x64。 我该如何解决我的问题?

【问题讨论】:

  • 你在哪里说数据是JSON?

标签: iphone ios objective-c asihttprequest nskeyedarchiver


【解决方案1】:

您获取 JSON 数据,因此您必须使用 NSJSONSerialization 转换该数据。看看类参考。

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

也可以查看Working with JSON

【讨论】:

    【解决方案2】:

    你可以像这样使用 NSJSONSerialization:

    NSError *jsonError = nil;
    NSArray *jsonObject = (NSArray *)[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多