我试图从网页加载一些JSON,它可以正常工作,但是我在自动合成属性和内容方面遇到了一些麻烦。
这是我的.h代码:
@interface MasterViewController : UITableViewController
@property (nonatomic, strong) NSArray *jsonList;
@property (nonatomic, strong) NSDictionary *jsonResults;
这是我的.m代码:
NSData *jsonText = [[NSData alloc] initWithBase64EncodedString: [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil] options:0];
self.jsonResults = [NSJSONSerialization JSONObjectWithData:jsonText options:NSJSONReadingMutableContainers error:&error];
//jsonResults contains 71 records.
self.jsonList = self.jsonResults.allKeys; //errors out here. [__NSArrayM allKeys]: unrecognized selector sent
JSON示例:
[{"客户ID":419,"客户名称":"奥尔福德""锁定":0," ExpenseReport& #34;:1},{"客户ID":600,"客户名称":"阿伦""锁定":0,&# 34; ExpenseReport":0},{"客户ID":18424"客户名称":"公园""锁定":0 " ExpenseReport":0}]
这是堆栈跟踪:
2017-04-04 11:08:04.798180 GWM[30923:6253087] [] nw_host_stats_add_src recv too small, received 24, expected 28
2017-04-04 11:08:04.888706 GWM[30923:6253087] [] ____nwlog_simulate_crash_inner_block_invoke dlopen CrashReporterSupport failed
2017-04-04 11:08:04.889985 GWM[30923:6253087] [] __nwlog_err_simulate_crash simulate crash failed "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-04-04 11:08:04.901282 GWM[30923:6253087] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
[x86_64] libnetcore-856.30.16
0 libsystem_network.dylib 0x000000010f2a4666 __nw_create_backtrace_string + 123
1 libnetwork.dylib 0x0000000110414006 nw_socket_add_input_handler + 3164
2 libnetwork.dylib 0x00000001103f1555 nw_endpoint_flow_attach_protocols + 3768
3 libnetwork.dylib 0x00000001103f0572 nw_endpoint_flow_setup_socket + 563
4 libnetwork.dylib 0x00000001103ef298 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2612
5 libnetwork.dylib 0x000000011040aae1 nw_endpoint_handler_path_change + 1261
6 libnetwork.dylib 0x000000011040a510 nw_endpoint_handler_start + 570
7 libnetwork.dylib 0x00000001104221f9 nw_endpoint_resolver_start_next_child + 2240
8 libdispatch.dylib 0x000000010f021978 _dispatch_call_block_and_release + 12
9 libdispatch.dylib 0x000000010f04b0cd _dispatch_client_callout + 8
10 libdispatch.dylib 0x000000010f028e17 _dispatch_queue_serial_drain + 236
11 libdispatch.dylib 0x000000010f029b4b _dispatch_queue_invoke + 1073
12 libdispatch.dylib 0x000000010f02c385 _dispatch_root_queue_drain + 720
13 libdispatch.dylib 0x000000010f02c059 _dispatch_worker_thread3 + 123
14 libsystem_pthread.dylib 0x000000010f4d74de _pthread_wqthread + 1129
15 libsystem_pthread.dylib 0x000000010f4d5341 start_wqthread + 13
2017-04-04 14:25:58.281111 GWM[30923:6338888] [] nw_socket_write_close shutdown(12, SHUT_WR): [57] Socket is not connected
2017-04-04 14:25:58.281978 GWM[30923:6338888] [] nw_endpoint_flow_service_writes [1.1 ready socket-flow (satisfied)] Write request has 0 frame count, 0 byte count
2017-04-04 14:25:58.282905 GWM[30923:6338894] [] __tcp_connection_write_eof_block_invoke Write close callback received error: [89] Operation canceled
2017-04-04 14:25:58.378 GWM[30923:6252853] -[__NSArrayM allKeys]: unrecognized selector sent to instance 0x600000247fe0
2017-04-04 14:25:58.396 GWM[30923:6338892] XPC connection interrupted
2017-04-04 14:25:59.741 GWM[30923:6252853] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM allKeys]: unrecognized selector sent to instance 0x600000247fe0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d1bcd4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010c7fd21e objc_exception_throw + 48
2 CoreFoundation 0x000000010d22cf04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010d142005 ___forwarding___ + 1013
4 CoreFoundation 0x000000010d141b88 _CF_forwarding_prep_0 + 120
5 GWM 0x000000010c11a29e -[MasterViewController viewDidLoad] + 1070
6 UIKit 0x000000010d782a3d -[UIViewController loadViewIfRequired] + 1258
7 UIKit 0x000000010d782e70 -[UIViewController view] + 27
8 UIKit 0x000000010d7d11fa -[UINavigationController preferredContentSize] + 198
9 UIKit 0x000000010dea4b3d -[UIViewController(UIPopoverController_Internal) _resolvedPreferredContentSize] + 62
10 UIKit 0x000000010deace79 -[UIPopoverController _transitionFromViewController:toViewController:animated:] + 146
11 UIKit 0x000000010dea53a8 -[UIPopoverController _initWithContentViewController:popoverControllerStyle:] + 887
12 UIKit 0x000000010dc160bc -[UISplitViewControllerClassicImpl _setupHiddenPopoverControllerWithViewController:] + 110
13 UIKit 0x000000010dc14b68 -[UISplitViewControllerClassicImpl _addOrRemovePopoverPresentationGestureRecognizer] + 121
14 UIKit 0x000000010dc1442e -[UISplitViewControllerClassicImpl loadView] + 349
15 UIKit 0x000000010d78261c -[UIViewController loadViewIfRequired] + 201
16 UIKit 0x000000010d782e70 -[UIViewController view] + 27
17 UIKit 0x000000010d64c4b5 -[UIWindow addRootViewControllerViewIfPossible] + 71
18 UIKit 0x000000010d64cc06 -[UIWindow _setHidden:forced:] + 293
19 UIKit 0x000000010d660519 -[UIWindow makeKeyAndVisible] + 42
20 UIKit 0x000000010d5d8f8d -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818
21 UIKit 0x000000010d5df0ed -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
22 UIKit 0x000000010d5dc26d -[UIApplication workspaceDidEndTransaction:] + 188
23 FrontBoardServices 0x00000001106d96cb __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
24 FrontBoardServices 0x00000001106d9544 -[FBSSerialQueue _performNext] + 189
25 FrontBoardServices 0x00000001106d98cd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
26 CoreFoundation 0x000000010d161761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
27 CoreFoundation 0x000000010d14698c __CFRunLoopDoSources0 + 556
28 CoreFoundation 0x000000010d145e76 __CFRunLoopRun + 918
29 CoreFoundation 0x000000010d145884 CFRunLoopRunSpecific + 420
30 UIKit 0x000000010d5daaea -[UIApplication _run] + 434
31 UIKit 0x000000010d5e0c68 UIApplicationMain + 159
32 GWM 0x000000010c123e9f main + 111
33 libdyld.dylib 0x000000010f09768d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
最终我想用表视图显示它,但是我还没有找到一种方法将NSDictionary绑定到表而不先将它放入NSArray ......
答案 0 :(得分:2)
您的JSON是一个字典数组。但是你的代码假定JSON是一个字典。
如果您需要的只是jsonResults
数组,我会摆脱不必要的jsonList
属性。
然后你的代码应该是:
NSData *jsonText = [[NSData alloc] initWithBase64EncodedString: [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil] options:0];
self.jsonList = [NSJSONSerialization JSONObjectWithData:jsonText options:0 error:&error];
if (self.jsonList == nil) {
NSLog(@"Error reading JSON: %@", error);
}