user10283685
user10283685

Reputation:

Could not cast value of type '__NSCFString' to 'NSDictionary'

I want to access some data of my Json that is inside another json

here is my code:

let extraData = userInfo["extraData"] as! [String : Any]
print(extraData["message_id"])

but I get below error while running:

Could not cast value of type '__NSCFString' (0x264300f90) to 'NSDictionary' (0x264301bc0)

here is my Json:

[AnyHashable("largeIcon"): http://test.png, AnyHashable("notifyType"): notifyData, AnyHashable("ledColor"): #f39c12, AnyHashable("extraData"): {"is_background":0,"message_id":"1156","deep_link":{"action_type":"U","url":"teknik://teknik"}}, AnyHashable("message"): test, AnyHashable("id"): 50368138, AnyHashable("vibrate"): 1, AnyHashable("gcm.message_id"): 0:1544436390847%bebba17fbebba17f, AnyHashable("autoRun"): false, AnyHashable("action"): {"type":"A","url":"Activity.MessageActivityJava"}, AnyHashable("sound"): 3, AnyHashable("title"): newtest, AnyHashable("aps"): { "content-available" = 1; }]

Upvotes: 0

Views: 3174

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

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)
}

as extraData value is a json string not a direct dictionary

Upvotes: 4

Related Questions