0

I am trying to read the following json:

[{"result":"1","msg":"Login Successful.”,”title":"Login","redirect":"index.php","servers":"{\"140\":\"10 minute Email\"}","server”:”xxx.xxx.xxx.xxx”}]

like so:

 NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);
                
                SBJsonParser *jsonParser = [SBJsonParser new];
                NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
                NSLog(@"%@",jsonData);
                NSInteger success = [(NSNumber *) [jsonData objectForKey:@"result"] integerValue];
                NSLog(@"%d",success);
                if(success == 1)
                {
                    NSLog(@"Login SUCCESS");
                    [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
                    
                } else {
                    
                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                    [self alertStatus:error_msg :@"Login Failed!"];
                }

but I am getting the following error:

2014-01-01 20:44:08.857 Server Monitor[9704:70b] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x8e59950

2014-01-01 20:44:08.857 Server Monitor[9704:70b] Exception: -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x8e59950

I think the problem is that the json is an array, how can I handle that?

Community
  • 1
  • 1
Waqleh
  • 9,741
  • 8
  • 65
  • 103
  • 2
    Why are you using sbjson? That's pretty outmoded approach. https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html – Rob Jan 01 '14 at 18:56
  • this is what I found by googling – Waqleh Jan 01 '14 at 18:58
  • Yeah Apple added class above in ios5. Also if you are using CoreData, you can use a framework for seamlessly going in and out of entities using JSON as a format for NSCoding (the built in serialization protocol). – Rob Jan 01 '14 at 18:59
  • The error message clearly states that what you think is a *dictionary*, is actually an *array*. – Martin R Jan 01 '14 at 19:00
  • possible duplicate of [SBJson Execptions after parsing (\_\_NSArrayM objectForKey:)](http://stackoverflow.com/questions/13460824/sbjson-execptions-after-parsing-nsarraym-objectforkey) – Martin R Jan 01 '14 at 19:00
  • @MartinR ya I noticed, how can I handle an array? – Waqleh Jan 01 '14 at 19:01
  • @Waqleh: Did you have a look at the NSArray documentation? (Hint: objectAtIndex:). - Also googling for "__NSArrayM objectForKey unrecognized selector sent to instance" should give many similar questions with answers. – Martin R Jan 01 '14 at 19:04

2 Answers2

1

The problem is your JSON's root object is an array:

[ … ]

but you're incorrectly assuming it's a dictionary:

NSDictionary *jsonData = (NSDictionary *)[jsonParser objectWithString:responseData error:nil];

You could do something like this if the response will always be an array with one object:

NSArray *jsonArray = (NSArray *)[jsonParser objectWithString:responseData error:nil];
NSDictionary *jsonData = [jsonArray lastObject];

But a safer approach is to inspect the class:

NSObject *object = [jsonParser objectWithString:responseData error:nil];
if ([object isKindOfClass:[NSArray class]]) {
    // it's an array …
} else if ([object isKindOfClass:[NSDictionary class]]) {
    // it's a dictionary …
}

Finally,

  • You should probably use NSJSONSerialization instead of SBJSON.
  • You should not pass nil in for the error argument; you should add error handling.
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

Use like this

NSString *theJSON = [request responseString];

 // Now we have successfully captured the JSON ouptut of our request.

// Alloc and initialize our JSON parser.

 SBJsonParser *parser = [[SBJsonParser alloc] init];

 // Actually parsing the JSON.

 NSMutableDictionary *jsonDictionary = [parser objectWithString:theJSON error:nil];

Happy coding

Bug
  • 2,576
  • 2
  • 21
  • 36