Parsing NSJSONReadingAllowFragments

I get some json data in my application:

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"json :%@", json);

which registers:

json :{
  "email" : "/apex/emailAttachment?documentId=00PZ0000000zAgSMAU&recipientId=003Z000000XzHmJIAV&relatedObjectId=a09Z00000036kc8IAA&subject=Pricing+Comparison"
}

This is exactly what I want.

However, when I move on to reading the email value, doing

[json objectForKey:@"email"]

I get an invalid argument exception:

Application termination due to unselected exception 'NSInvalidArgumentException', reason: '* - [NSDictionary initWithDictionary: copyItems:]: dictionary is not NSDictionary'

How can I read this value?

+4
source share
2 answers

It seems that your server is sending "embedded JSON": jsonResponse- a JSON string (not a Dictionary). The value of this string is again the JSON data representing the dictionary.

JSON :

NSString *jsonString = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingAllowFragments error:nil];
NSData *innerJson = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:innerJson options:0 error:nil];

NSString *email = jsonDict[@"email"];
+4

'json', , , .

NSJSONReadingAllowFragments JSONObjectWithData:options:error:, :

, , NSArray NSDictionary.

, .

, , mutable . , NSJSONReadingMutableContainers /dics NSJSONReadingMutableLeaves

+1

All Articles