Question

I am trying to convert NSDictionary to JSON object to send it over network. But I am getting an error in converting dictionarty to NSData. Here is my code:

NSError *error=nil;
        NSDictionary *loginfbDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

        NSData *d=[NSJSONSerialization dataWithJSONObject:loginfbDictionary options:NSJSONWritingPrettyPrinted error:&error];
        NSLog(@"error===>%@",error);
 BOOL f=[NSJSONSerialization isValidJSONObject:d];

When I log my dictionary befor converting it into NSData I got it printed. But When I wrote that code after converting dictionary into data I got nothing. I put Debug point after NSData *d... But my code is not executing after that line. I am writing this code in appdelegate file.

Object Contain:

  NSArray *keys = [NSArray arrayWithObjects:
                         @"loginVia",
                         @"userName",
//                             @"password",
                         @"firstName",
                         @"lastName",
                         @"gender",
                         @"dob",
                         @"email",
                         @"fb_url",
                         nil];

        NSArray *objects = [NSArray arrayWithObjects:
                            @"facebook",
                            [[NSUserDefaults standardUserDefaults] objectForKey:@"username"],
 //                                [[NSUserDefaults standardUserDefaults] objectForKey:@"password"],
                            [FBUserProfileData objectForKey:@"first_name"],
                            [FBUserProfileData objectForKey:@"last_name"],
                            [FBUserProfileData objectForKey:@"gender"],
                            ds,
                            [FBUserProfileData objectForKey:@"email"],
                            fb_url,
                            nil];
Was it helpful?

Solution

It looks like you're adding an NSDate or an NSURL object into your dictionary to be converted to JSON. The problem is that JSON doesn't have a "Date" or a "URL" data type. So you need to convert the date / URL into a string (NSString) and add that instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top