我想将json数据放到一个块中,但它失败了。
互联网请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *dataResult, NSError *connectionError) {
if (!connectionError) {
if (successBlock) {
//NSString* str = [[NSString alloc] initWithData:dataResult encoding:NSUTF8StringEncoding];
//NSLog(@"response data: %@", str);
NSDictionary *json = [[NSDictionary alloc]init];
json = [NSJSONSerialization JSONObjectWithData:dataResult options:kNilOptions error:nil];
successBlock(json);
}
} else {
if (failureBlock) {
failureBlock(connectionError);
}
}
}];
successBlock定义如下:
typedef void(^HttpRequestSuccessBlock)(id responseObject);
typedef void(^HttpRequestFailBlock)(NSError *error);
我可以成功获取dataResult
中的1
并将其转换为NSString,我NSlog
,它实际上是Json
格式。
但是当运行代码时,它崩溃并显示以下错误:
2017-08-21 09:43:22.258 shopiPhoneDemo[24397:5799646] -[__NSDictionaryI bytes]: unrecognized selector sent to instance 0x7faa75501220
2017-08-21 09:43:22.264 shopiPhoneDemo[24397:5799646] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI bytes]: unrecognized selector sent to instance 0x7faa75501220'
更新堆栈跟踪:
2017-08-21 09:43:22.258 shopiPhoneDemo[24397:5799646] -[__NSDictionaryI bytes]: unrecognized selector sent to instance 0x7faa75501220
2017-08-21 09:43:22.264 shopiPhoneDemo[24397:5799646] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI bytes]: unrecognized selector sent to instance 0x7faa75501220'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105e8cb0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001058f1141 objc_exception_throw + 48
2 CoreFoundation 0x0000000105efc134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000105e13840 ___forwarding___ + 1024
4 CoreFoundation 0x0000000105e133b8 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001054281a1 -[_NSJSONReader findEncodingFromData:withBOMSkipLength:] + 46
6 Foundation 0x0000000105428087 -[_NSJSONReader parseData:options:] + 58
7 Foundation 0x0000000105427fbb +[NSJSONSerialization JSONObjectWithData:options:error:] + 139
8 shopiPhoneDemo 0x0000000104cc4e2d __24-[AppDelegate youtuTest]_block_invoke + 77
9 shopiPhoneDemo 0x0000000104cbfc3f __62-[TXQcloudFrSDK sendRequest:mothod:successBlock:failureBlock:]_block_invoke + 271
10 CFNetwork 0x0000000108a045ae __67+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]_block_invoke_2 + 161
11 Foundation 0x00000001053fb3b7 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
12 Foundation 0x00000001053fb0bb -[NSBlockOperation main] + 101
13 Foundation 0x00000001053f9877 -[__NSOperationInternal _start:] + 627
14 Foundation 0x00000001053f55fc __NSOQSchedule_f + 198
15 libdispatch.dylib 0x000000010940905c _dispatch_client_callout + 8
16 libdispatch.dylib 0x00000001093ea40b _dispatch_main_queue_callback_4CF + 411
17 CoreFoundation 0x0000000105e51909 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
18 CoreFoundation 0x0000000105e17ae4 __CFRunLoopRun + 2164
19 CoreFoundation 0x0000000105e17016 CFRunLoopRunSpecific + 406
20 GraphicsServices 0x000000010b4bba24 GSEventRunModal + 62
21 UIKit 0x0000000106999134 UIApplicationMain + 159
22 shopiPhoneDemo 0x0000000104cd0eff main + 111
23 libdyld.dylib 0x000000010945565d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Json
NSLog是:
{"errorcode":0,"errormsg":"OK","session_id":"","name":"陈佳佳","name_confidence_all":[99,99,99],"sex":"女","sex_confidence_all":[99],"nation":"汉","nation_confidence_all":[99],"birth":"1985/9/11","birth_confidence_all":[100,100,100,100,100,100,100,100,100],"address":"福建省尤溪县汤川乡汤三村16号","address_confidence_all":[99,99,99,99,99,99,99,99,99,99,99,99,99,99,99],"id":"350426198509113027","id_confidence_all":[100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100],"frontimage":"","frontimage_confidence_all":[],"watermask_confidence_all":[],"valid_date_confidence_all":[],"authority_confidence_all":[],"backimage_confidence_all":[],"detail_errorcode":[],"detail_errormsg":[]}
我使用json编辑器格式是:
{
"errorcode": 0,
"errormsg": "OK",
"session_id": "",
"name": "陈佳佳",
"name_confidence_all": [
99,
99,
99
],
"sex": "女",
"sex_confidence_all": [
99
],
"nation": "汉",
"nation_confidence_all": [
99
],
"birth": "1985/9/11",
"birth_confidence_all": [
100,
100,
100,
100,
100,
100,
100,
100,
100
],
"address": "福建省尤溪县汤川乡汤三村16号",
"address_confidence_all": [
99,
99,
99,
99,
99,
99,
99,
99,
99,
99,
99,
99,
99,
99,
99
],
"id": "350426198509113027",
"id_confidence_all": [
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100,
100
],
"frontimage": "",
"frontimage_confidence_all": [],
"watermask_confidence_all": [],
"valid_date_confidence_all": [],
"authority_confidence_all": [],
"backimage_confidence_all": [],
"detail_errorcode": [],
"detail_errormsg": []
}
我试图做的事情:
mutablecopy
制作json
,但它不起作用。id
更改为NSDictionary
,但它也没有。答案 0 :(得分:0)
您可能正在处理NSArray
而不是NSDictionary
。因此,处理这两种情况会更好。
id json = [NSJSONSerialization JSONObjectWithData:dataResult options:kNilOptions error:nil];
if ([json isKindOfClass:[NSArray class]]) {
NSLog(@"its an array!");
NSArray *jsonArray = (NSArray *)json;
NSLog(@"jsonArray - %@",jsonArray);
}
else {
NSLog(@"its probably a dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)json;
NSLog(@"jsonDictionary - %@",jsonDictionary);
}
答案 1 :(得分:0)
我认为数据内部或json parcer内部存在问题。尝试使用其他json解析器和开放代码,例如https://github.com/johnezang/JSONKit/blob/master/JSONKit.h。你可以找到,究竟是什么发送了这个例外。