-1

How can I convert from String to a Dictionary in swift

my code

Alamofire.request(apiMethod).responseJSON { response in
        let statCode = response.response?.statusCode
        print("URLRequest: \(String(describing: response.request!))")
        print("RequestSuccess: \(String(describing: response.result.isSuccess))")
        print("statusCode: \(statCode!)")
        print("Response: \(String(describing: response.response!))")
        print("Result: \(response.result)")

        if let json = response.result.value {
            print("JSON: \(json)")
        }

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {

            print(utf8Text) 


        }

    }

=======================

out code is my String I need to change to Dictionary :

{"result":[{"category":2,"name":"test","subCategories":[],"id":2},{"categoryType":1,"name":"التعليم والتدريب","subCategories":[{"categoryId":1,"name":"التعليم","categoryName":"التعليم والتدريب","id":1},{"categoryId":1,"name":"التدريب","categoryName":"التعليم والتدريب","id":2}],"id":1}],"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}

  • And what are the others prints output? At some point with Alamofire since you used `.responseJSON{}` you should have already a serialized JSON into a Dictionary. – Larme Jul 10 '18 at 07:52
  • `if let json = response.result.value { print("JSON: \(json)") }` . - Look in this block for json. – Krunal Jul 10 '18 at 07:59

1 Answers1

-2
let dictionary = try? JSONSerialization.jsonObject(with: string, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any]
grovile
  • 45
  • 1
  • 8