-2

I'm getting a json in a NSDictionary. I am watching this json if I do a NSLog of the NSDictionary.

NSLog->>  {"login":{"pass":"yeeply123","user":"Yeeply"}}

I get places dictionary here:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSError *thisError;


    NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:myConnectionData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&thisError];
    NSLog(@"Prueba %@", parsedObject);
    [self.delegate requestJSONFinishedWithParsedObject:placesDictionary];
}

and I pass it to other function as placesDictionary But when I try to get data from this NSDictionary with this sentence:

 NSDictionary *userDictionary= [placesDictionary objectForKey:@"login"];
 NSString *pass=  [userDictionary objectForKey:@"pass"];

I get an error like this:

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7172f10

I don't know what is happening, I did it in other project and it worked..

Thank you

roxeman3
  • 147
  • 1
  • 3
  • 12

2 Answers2

0

Your JSON is an array of dictionaries. Make sure that the placesDictionary is a dictionary, from the error it seems like it is a string. There is no error in your code.

EDIT:

From the log placesDictionary is an array.

NSDictionary *dict = placesDictionary[0];
NSString *itemToPassBack = dict[@"pass"];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

You server does not sent valid JSON. What you get from the server is

"[{\"pass\":\"example23\"},{\"user\":\"example\"}]"

which is a JSON string (containing JSON data). So the top-level object is not a dictionary or array, which is invalid according to the JSON specification.

Your call

[NSJSONSerialization JSONObjectWithData:myConnectionData:...]

succeeds only because because of the the NSJSONReadingAllowFragments option, otherwise it would fail.

The string itself contains valid JSON data, so what you can do is to apply another JSON parsing operation to the contents of the string:

NSString *parsedObject = [NSJSONSerialization JSONObjectWithData:myConnectionData options:NSJSONReadingAllowFragments error:&thisError];
NSData *innerJson = [parsedObject dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *innerObject = [NSJSONSerialization JSONObjectWithData:innerJson options:NSJSONReadingMutableContainers error:&thisError];

The innerObject is now an array of two dictionaries, which you can access like:

NSString *pass = [[innerObject objectAtIndex:0] objectForKey:@"pass"];

(Of course a better solution would be to fix the server to send proper JSON.)

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I fixed the server. Now, I have this dictionary(This is a NSlog output)-> {"login":{"pass":"example123","user":"example"}} Im using this code to get the data: `NSDictionary *userDictionary= [placesDictionary objectForKey:@"login"]; NSString *pass= [userDictionary objectForKey:@"pass"];` and it still does not work – roxeman3 Apr 19 '13 at 11:32
  • @roxeman3: That does not look like a dictionary! It still looks like a string. Did you try my code? – Martin R Apr 19 '13 at 11:33
  • Sorry, I didn't try it because I want to solve as a Dictionary. Do you mean that this {"login":{"pass":"yeeply123","user":"Yeeply"}} is not a dictionary? I checked it in a json validator and everything is ok.. – roxeman3 Apr 19 '13 at 11:37
  • @roxeman3: That is a **string** containing valid JSON! The NSLog output of a dictionary would look like `{ pass = yeeply123; user = Yeeply }`. – Martin R Apr 19 '13 at 11:42