微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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"];

原文地址:https://www.jb51.cc/iOS/335494.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐