Iphone 帮助我使用JSONTouch解析JSON值
我有以下json: 我想通过下面的代码获取此数据中的所有名称,但它给出了一个错误。我怎样才能修好它Iphone 帮助我使用JSONTouch解析JSON值,iphone,objective-c,Iphone,Objective C,我有以下json: 我想通过下面的代码获取此数据中的所有名称,但它给出了一个错误。我怎样才能修好它 NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSDictionary *results = [responseString JSONValue]; NSDictionary *users = [results objectFo
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [responseString JSONValue];
NSDictionary *users = [results objectForKey:@"users"] objectForKey:@"user"];
for (NSDictionary *user in users){
//NSLog(@"key:%@, value:%@", user, [user objectForKey:user]);
NSString *title = [users objectForKey:@"NAME"];
NSLog(@"%@", title);
}
错误:
2011-01-29 00:18:50.386 NeighborMe[7763:207] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xb618840
2011-01-29 00:18:50.388 NeighborMe[7763:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xb618840'
*** Call stack at first throw:
(
0 CoreFoundation 0x027d9b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0292940e objc_exception_throw + 47
2 CoreFoundation 0x027db6ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0274b2b6 ___forwarding___ + 966
4 CoreFoundation 0x0274ae72 _CF_forwarding_prep_0 + 50
5 NeighborMe 0x0000bd75 -[NeighborListViewController connectionDidFinishLoading:] + 226
6 Foundation 0x0007cb96 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108
7 Foundation 0x0007caef _NSURLConnectionDidFinishLoading + 133
8 CFNetwork 0x02d8d72f _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 285
9 CFNetwork 0x02e58fcf _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 389
10 CFNetwork 0x02e5944b _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 1537
11 CFNetwork 0x02d82968 _ZN19URLConnectionClient13processEventsEv + 100
12 CFNetwork 0x02d827e5 _ZN17MultiplexerSource7performEv + 251
13 CoreFoundation 0x027bafaf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
14 CoreFoundation 0x0271939b __CFRunLoopDoSources0 + 571
15 CoreFoundation 0x02718896 __CFRunLoopRun + 470
16 CoreFoundation 0x02718350 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x02718271 CFRunLoopRunInMode + 97
18 GraphicsServices 0x030b800c GSEventRunModal + 217
19 GraphicsServices 0x030b80d1 GSEventRun + 115
20 UIKit 0x002e9af2 UIApplicationMain + 1160
21 NeighborMe 0x00001c34 main + 102
22 NeighborMe 0x00001bc5 start + 53
23 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
我真的只是想能够遍历名称在我看来,当你创建“用户”词典时,你实际上是在创建“用户”词典
NSDictionary *users = [[results objectForKey:@"users"] objectForKey:@"user"];//crating users dictionary with 1 "user" inside.
编辑
第二种观点。为什么不迭代“结果”字典呢?
那样-
for (NSDictionary *user in result){
//NSLog(@"key:%@, value:%@", user, [user objectForKey:user]);
NSString *title = [users objectForKey:@"NAME"];
NSLog(@"%@", title);
}
希望能有所帮助
shani在我看来,当你创建“用户”词典时,你实际上是在创建“用户”词典
NSDictionary *users = [[results objectForKey:@"users"] objectForKey:@"user"];//crating users dictionary with 1 "user" inside.
编辑
第二种观点。为什么不迭代“结果”字典呢?
那样-
for (NSDictionary *user in result){
//NSLog(@"key:%@, value:%@", user, [user objectForKey:user]);
NSString *title = [users objectForKey:@"NAME"];
NSLog(@"%@", title);
}
希望能有所帮助
沙尼
-[\uu NSArrayM objectForKey:]:无法识别的选择器发送到实例异常的原因
第一步是理解JSON数据。其结构如下:
NSString *responseString = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
// 1. the top level value is an array
NSArray *results = [responseString JSONValue];
// 2. each element in the array is an object/dictionary with
// a single key called "user"
for (NSDictionary *element in results) {
// 3. the value of the "user" key is itself another object/dictionary
// with various key-value pairs
NSDictionary *user = [element objectForKey:@"user"];
NSString *title = [user objectForKey:@"NAME"];
NSLog(@"%@", title);
}
-[\uu NSArrayM objectForKey:]:无法识别的选择器发送到实例异常的原因
第一步是理解JSON数据。其结构如下:
NSString *responseString = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
// 1. the top level value is an array
NSArray *results = [responseString JSONValue];
// 2. each element in the array is an object/dictionary with
// a single key called "user"
for (NSDictionary *element in results) {
// 3. the value of the "user" key is itself another object/dictionary
// with various key-value pairs
NSDictionary *user = [element objectForKey:@"user"];
NSString *title = [user objectForKey:@"NAME"];
NSLog(@"%@", title);
}
您的答案与OP使用的JSON数据不匹配。您的答案与OP使用的JSON数据不匹配。