1

I am parsing JSON data with JSONKit as NSMutableDictionary.

NSString *str = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

NSMutableDictionary *jsonResponse = [self.responseData objectFromJSONData];

NSMutableDictionary *newData = [[NSMutableDictionary alloc] init];
[newData addEntriesFromDictionary:[jsonResponse mutableCopy]];

When i do this i am getting this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableDictionary addEntriesFromDictionary:]: dictionary argument is not an NSDictionary'

I am trying to figure out what is causing this problem. I know that jsonResponse is an object of JKArray from my other experience.

I need help. Thanks.

Umut Sirin
  • 2,462
  • 1
  • 21
  • 31

1 Answers1

0

Try the following:

id object = [self.responseData objectFromJSONData];
NSLog(@"%@", [object class]);

Most likely your response is an array instead of a dictionary.


If you really want to convert the array into a dictionary, you could do something like this, using a self-defined key:

NSArray *array = [self.responseData objectFromJSONData];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:array forKey:@"posts"];

Though perhaps there are some better options if you could show me the contents of your array.

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • response is JKArray, like i said before. – Umut Sirin Feb 12 '13 at 01:02
  • Well, then that's the problem, you can't convert an array directly into a dictionary. What do you want to achieve by putting the data into a dictionary? – Wolfgang Schreurs Feb 12 '13 at 01:03
  • The thing is every code i wrote is using the dictionary methods such as [[[self.eventList valueForKeyPath:@"picture"] valueForKeyPath:@"url"] objectAtIndex:indexPath.row]; They working successfully, but when i try to copy it, it starts to work like an array and doesn't work. I dont know how to achieve this. I just want to retrieve recent posts from server and prepend it to my data. – Umut Sirin Feb 12 '13 at 01:07
  • It sounds like you could benefit from a [review](http://www.kirupa.com/html5/hashtables_vs_arrays.htm) of the difference between a hashtable (that's what `NSDictionary` is) and an array (like `NSArray`). There are lots of google results for "array data structure" and "hashtable data structure" online, so take your pick of articles and blogs to read from -- I link to one above that compares the two structures. – Mathew Feb 12 '13 at 03:03