ios - 无法将“__NSCFString”类型的值转换为“NSDictionary”
问题描述
我想访问另一个 json 中的我的 Json 的一些数据
这是我的代码:
let extraData = userInfo["extraData"] as! [String : Any]
print(extraData["message_id"])
但我在运行时遇到以下错误:
Could not cast value of type '__NSCFString' (0x264300f90) to 'NSDictionary' (0x264301bc0)
这是我的Json:
解决方案
你可以试试
do {
let dd = userInfo["extraData"] as! String
let con = try JSONSerialization.jsonObject(with: dd.data(using: .utf8)!, options: []) as! [String:Any]
print(con["message_id"])
catch {
print(error)
}
因为extraData
值是 json 字符串而不是直接字典
推荐阅读
- java - 为什么它不等于 String 和 String From List
- algorithm - 如何安排任务
- r - 保留满足一个标准的行,如果满足另一个标准,则保留其上方的行
- php - 单击图像以在 HTML 页面中打开新选项卡
- bash - 迭代时无法从文件中替换字符串
- javascript - 在循环javascript上附加参数
- amazon-web-services - 使用带有参数的 Fn::Join 时出现 AWS Cloudformation 错误
- python - xpath contains() 不起作用:返回包含文本的每个节点,即文本不为空的地方
- ios - iOS TableView自定义单元格不显示任何内容
- c++ - 将 caffe 的 PriorBox 导入 TensorRT