0

I just fetched data but when i try to append it to string from NSData , compiler occurs Exception error . My code seems like this:

NSDictionary *allDAtaDictionary = [NSJSONSerialization JSONObjectWithData:_webData options:0 error:nil];
NSString *jSonStatus = [allDAtaDictionary objectForKey:@"status"];
if([jSonStatus isEqualToString:@"OK"])
{
   NSDictionary *results = [allDAtaDictionary objectForKey:@"user_details"];
   NSLog(@"User Details :  %@",results);

All things works well until these line just starts:

NSString *userID = [results objectForKey:@"userID"];

The error output is :

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x9485a00'

and this is my jSOn output , NSLog(@"User Details : %@",results);

User Details :  (
        {
        userCity = California;
        userCountry = "United States";
        userDOB = "1988-03-02";
        userDetails = "";
        userEmail = "xxx@xxx.com";
        userFBID = 715296184;
        userFullName = "John Mc Grager";
        userGender = Male;
        userID = 70;
        userLastLoginTime = "2013-05-29 10:51:27";
        userLatitude = "37.7858";
        userLongtitude = "-122.406";
        userNickName = warblader;
        userPassword = "";
        userStatus = 1;
        userToken = "";
    }
)
Onder OZCAN
  • 1,556
  • 1
  • 16
  • 36
  • 3
    If I had a nickel for every time someone asked this same question...Well, the clue is right at the end of the first line above! – borrrden May 29 '13 at 07:59
  • What do you think `__NSCFArray` means? – borrrden May 29 '13 at 08:01
  • @borrrden , I wish you would read carefully what i have asked for help... – Onder OZCAN May 29 '13 at 08:16
  • I read it well enough: you failed to understand the object you were getting and were treating it as a dictionary instead of an array (i.e. `__NSCFArray` which is the huge clue to the entire problem). Unlike the poster below I don't plan to hand you such an easy answer on a silver platter. Instead I think it would be better for you to understand what went wrong. – borrrden May 29 '13 at 08:20
  • ye ye u wrote nickel ... instead of write something, very brave – Onder OZCAN May 29 '13 at 13:03

3 Answers3

3

Seems like the JSON is an array with one dictionary in it. What you show us here at least.

So

NSArray *results = [allDAtaDictionary objectForKey:@"user_details"];
NSLog(@"User Details :  %@",results);

assert(results.count>0);
NSDictionary *user = [results objectAtIndex:0];

NSString *userID = [results objectForKey:@"userID"];
NSLog(@"%@", userID);
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • You made my day ! Services pushes me data with in array's 1. element than I have to convert it in to Dictionary of 0. th array index. – Onder OZCAN May 29 '13 at 08:15
1

First of all you need save use the serialised data into an array.. Then you have to use it accordingly .. in this case

NSArray *allDAtaDictionary = [NSJSONSerialization JSONObjectWithData:_webData options:0 error:nil];

Log the array in index 0 and the see what you are getting. After this you can start parsing the data.

The exception is occuring due to this. Check my answer here to parse JSON How to parse JSON with multiple instance in Object C

Community
  • 1
  • 1
Sharanya K M
  • 1,805
  • 4
  • 23
  • 44
0

Your response probably has An array in it, which result Dictionary is being holding, so you need to use ObjectAtIndex rather calling objectForKey

Jamal Zafar
  • 2,179
  • 2
  • 27
  • 32