ios – 解析NSJSONReadingAllowFragments

我在我的应用程序中收到一些json数据:
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonResponse options:NSJSONReadingallowFragments error:nil];
        NSLog(@"json :%@",json);

哪些日志:

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

这正是我想要的.

但是,当我通过这样做去阅读电子邮件的价值时

[json objectForKey:@“email”]

我收到一个无效的参数异常:

Terminating app due to uncaught exception ‘NSinvalidargumentexception’,reason: ‘* -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not an NSDictionary’

我该如何读取这个值?

解决方法

您的服务器似乎发送“嵌套JSON”:jsonResponse是一个JSON字符串(不是
字典).该字符串的值也是表示字典的JSON数据.

在这种情况下,您必须将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"];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...