ASIHTTPRequest响应数据转换成的NSArray
我使用ASIHTTPRequest从服务器获取JSON响应。我想将此响应转换为NSArray。我的代码:ASIHTTPRequest响应数据转换成的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数据,因此您必须使用NSJSONSerialization转换该数据。看看课程参考。
还要检查这个Working with JSON
好的答案!谢谢! – RomanHouse 2013-02-14 14:27:18
您可以使用NSJSONSerialization这样的:
NSError *jsonError = nil;
NSArray *jsonObject = (NSArray *)[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
这也是正确的答案,谢谢! – RomanHouse 2013-02-14 14:27:46
你在哪里说的数据是JSON? – Vertig0 2013-02-14 13:45:12