0

I have the following json:

http://www.adityaherlambang.me/webservice.php?user=2&num=10&format=json

I would like to get all the name in this data by the following code, but it gives an error. How can I fix it?

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);
 }

Error:

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'

I really just wanted to be able to iterate through the names

aherlambang
  • 14,290
  • 50
  • 150
  • 253

2 Answers2

3
NSDictionary *results = [responseString JSONValue];

NSDictionary *users = [results objectForKey:@"users"] objectForKey:@"user"];
  1. The JSON data has an array as its top level value. It’s not a JSON object, hence it’s not a dictionary. This is why you get the -[__NSArrayM objectForKey:]: unrecognized selector sent to instance exception.
  2. There is no "users" entry in your JSON data.
  3. The code above doesn’t compile.

The first step is to understand your JSON data. It is structured as follows:

  1. the top level value is an array
  2. each element in the array is an object/dictionary with a single key called "user"
  3. the value of the "user" key is itself another object/dictionary with various key-value pairs

If you want to iterate over the users and print the value for the "NAME" key, follow the example below.

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);
}
1

It seems to me that when you create the "users" dictionary you are actually creating a "user" dictionary.

 NSDictionary *users = [[results objectForKey:@"users"] objectForKey:@"user"];//crating users dictionary with 1 "user" inside.

EDIT

in second view. why don't you just iterate the "result" dictionary? like that -

 for (NSDictionary *user in result){
  //NSLog(@"key:%@, value:%@", user, [user objectForKey:user]);
  NSString *title = [users objectForKey:@"NAME"];
  NSLog(@"%@", title);
 }

hope it will help shani

shannoga
  • 19,649
  • 20
  • 104
  • 169