1

I'm making URL Request to an API but I dont know how to render the JSON, It generates an array of multiple users like this [{"user": "value"}, {"user":"value"}] and I was trying to use a TableView so I need an NSDictionary but i think is better to render a JSON like {users: [{"user": "value"}, {"user":"value"}]}. I have this code to make the request

#import "JSONKit.h"
NSError *error = nil;
NSURLResponse *response = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost:3000/getusers"]];
[request setHTTPMethod:@"GET"];
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
users = [[jsonData objectFromJSONData] objectForKey:@"users"];
usersKeys = [users allKeys];

but I'm getting this error

2012-09-16 18:51:11.360 tableview[2979:c07] -[JKArray allKeys]: unrecognized selector sent to instance 0x6d30180 2012-09-16 18:51:11.362 tableview[2979:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JKArray allKeys]: unrecognized selector sent to instance 0x6d30180'

I dont really know how to accomplish this so any help is useful, thanks

danielbeard
  • 9,120
  • 3
  • 44
  • 58
jtomasrl
  • 1,430
  • 3
  • 13
  • 22

2 Answers2

2

You are getting that error because whatever got parsed out of "jsonData" isn't necessarily what you expected (i.e. a dictionary).

Perhaps you need some error checking in that code of yours.

For example:

NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(jsonData)
{
    id objectReturnedFromJSON = [jsonData objectFromJSONData];
    if(objectReturnedFromJSON)
    {
        if([objectReturnedFromJSON isKindOfClass:[NSDictonary class]])
        {
            NSDictionary * dictionaryFromJSON = (NSDictionary *)objectReturnedFromJSON;
            // assuming you declared "users" & "usersKeys" in your interface,
            // or somewhere else in this method
            users = [dictionaryFromJSON objectForKey:@"users"];
            if(users)
            {
                usersKeys = [users allKeys];
            } else {
                NSLog( @"no users in the json data");
            }
        } else {
            NSLog( @"no dictionary from the data returned by the server... check the data to see if it's valid JSON");
        }
    } else {
        NSLog( @"nothing valid returned from the server...");
    }
} else {
    NSLog( @"no data back from the server");
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

I was thinking on something like this

NSError *error = nil;
NSURLResponse *response = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost:3000/getusers"]];
[request setHTTPMethod:@"GET"];
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

JSONDecoder *decoder = [[JSONDecoder alloc]
                        initWithParseOptions:JKParseOptionNone];
NSArray *json = [decoder objectWithData:jsonData];

NSMutableArray *objects = [[NSMutableArray alloc] init];
NSMutableArray *keys = [[NSMutableArray alloc] init];
for (NSDictionary *user in json) {
    [objects addObject:[user objectForKey:@"user" ]];
    [keys addObject:[user objectForKey:@"value" ]];
}
users = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
NSLog(@"users: %@", users);
usersKeys = [users allKeys];

But it doesnt look efficient for many items or im wrong?

jtomasrl
  • 1,430
  • 3
  • 13
  • 22