I have a variable that contains an array of JSON that I put there
How could I decode it?

var dist: [String:Any] = [:] 

...

 let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) if let responseJSON = responseJSON as? [String: Any] { print(responseJSON) self.dist = responseJSON as [String: Any] self.performSegue(withIdentifier: "LoginSegue", sender: self.dist) 

...

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "LoginSegue" { let tabVc = segue.destination as! UITabBarController let navVc = tabVc.viewControllers!.first as! UINavigationController let chatVc = navVc.viewControllers.first as! MainViewController chatVc.json = dist } } 
  • Possible duplicate question: How to parse a complex array in swift 4? - Andrey Iskamov
  • No, I mean how to get json out of a variable - Marshall
  • and how did you put him there? - Andrey Iskamov
  • Well, look, I have an authorization form. After successful authorization, json is written to a variable and sent to the next controller. So everything is well transmitted. How now to extract this variable and then how to decode it - Marshall
  • You can see the code as you write to a variable and transfer it - Andrey Iskamov '17

1 answer 1

so you don’t need to decode it, you already have a dictionary in the variable, just get the json["my_key_string"] data by key, or create an object that will accept [String:Any] in the network and parse it by key like MyModel.init(jsonData: json)