Converting NSString to NSDictionary / JSON Converting NSString to NSDictionary / JSON json json

Converting NSString to NSDictionary / JSON


I believe you are misinterpreting the JSON format for key values. You should store your string as

NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":65,\"type\":\"text\"}}";NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

Now if you do following NSLog statement

NSLog(@"%@",[json objectForKey:@"ID"]);

Result would be another NSDictionary.

{    Content = 268;    type = text;}

Hope this helps to get clear understanding.


I think you get the array from response so you have to assign response to array.

NSError *err = nil;NSArray *array = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];NSDictionary *dictionary = [array objectAtIndex:0];
NSString *test = [dictionary objectForKey:@"ID"];
NSLog(@"Test is %@",test);


Use this code where str is your JSON string:

NSError *err = nil;NSArray *arr =  [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding]                                  options:NSJSONReadingMutableContainers                                    error:&err];// access the dictionariesNSMutableDictionary *dict = arr[0];for (NSMutableDictionary *dictionary in arr) {  // do something using dictionary}