首页 > 解决方案 > 无法将“__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:

{“内容可用”= 1;}]

标签: iosjsonswiftdata-extraction

解决方案


你可以试试

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 字符串而不是直接字典


推荐阅读