首页 文章

使用带有[]和不带[]的json

提问于
浏览
-3

当我用[]( like [{"ID":"1","name":"Amid","surname":"Nakano"}] )使用json时,我收到此错误 . 当没有[]( {"ID":"1","name":"Amid","surname":"Nakano"} )使用时,我不会得到错误;

代码是:

** NSData * jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@“http://yrishi.com/site/test/json.json”]];

id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
NSArray *keys = [jsonObjects allKeys];

// value in key name
//NSString *strMemberID = [jsonObjects objectForKey:@"MemberID"];
NSString *strName = [jsonObjects objectForKey:@"name"];
//NSString *strTel = [jsonObjects objectForKey:@"Tel"];
//NSLog(@"MemberID = %@",strMemberID);
NSLog(@"Name = %@",strName);
//NSLog(@"Tel = %@",strTel);

NSLog(@"====================");

// values in foreach loop
for (NSString *key in keys) {
    NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);

} **

Error is:

2015-03-09 00:43:34.146 iPhone显示图像来自JSON URL [6738:193615] - [ **NSArrayM allKeys]: unrecognized selector sent to instance 0x7fa002561ce0 2015-03-09 00:43:34.152 iPhone Display Image on Table View from JSON URL[6738:193615] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM allKeys]: unrecognized selector sent to instance 0x7fa002561ce0' *** First throw call stack: ( 0 CoreFoundation 0x0000000104229f35 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000103ec2bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010423104d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010418927c __forwarding** 988 4 CoreFoundation 0x0000000104188e18 _CF_forwarding_prep_0 120 5 iPhone显示图像来自JSON URL 0x00000001039962ab - [ViewController viewDidLoad] 299 6 UIKit 0x000000010473ba90 - [UIViewController loadViewIfRequired] 738 7 UIKit 0x000000010473bc8e - [UIViewController视图] 27 8 UIKit 0x000000010465aca9 - [UIWindow addRootViewControllerViewIfPossible] 58 9 UIKit 0x000000010465b041 - [UIWindow _setHidden:forced:] 247 10 UIKit 0x000000010466772c - [UIWindow makeKeyAndVisible] 42 11 UIKit 0x0000000104612061 - [UIApplication _callInitializationDelegatesForMainScene:transitionContext:] 2628 12 UIKit 0x0000000104614d2c - [UIApplication _runWithMainScene:transitionContext:completion:] 1350 13 UIKit 0x0000000104613bf2 - [UIApplication workspaceDidEndTransaction:] 179 14 FrontBoardServices 0x0000000106de52a3 31-[FBSSerialQueue performAsync:]_block_invoke + 16 15 CoreFoundation 0x000000010415f53c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK 12 16 CoreFoundation 0x0000000104155285 __CF RunLoopDoBlocks 341 17 CoreFoundation 0x0000000104155045 __CFRunLoopRun 2389 18 CoreFoundation 0x0000000104154486 CFRunLoopRunSpecific 470 19 UIKit 0x0000000104613669 - [UIApplication _run] 413 20 UIKit 0x0000000104616420 UIApplicationMain 1282 21 iPhone显示图像表格视图来自JSON URL 0x0000000103996a63 main 115 22 libdyld.dylib 0x00000001067b9145 start 1)libc abi . dylib:以NSException类型的未捕获异常终止(lldb)

1 回答

  • 1

    在一种情况下,JSON数据是包含字典的数组 . 在另一种情况下,它是一本字典 . 很明显,如果给出一个数组而不是字典,那么调用allKeys的代码将无法工作(实际上它会崩溃) .

相关问题