如何在swift 中从String
转换为Dictionary
我的代码
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代码是我的String
我需要更改为Dictionary
:
{"结果":〔{"类别":2,"名称":"测试","子类别":〔〕,"id":2},{"categoryType":1,"name":"التعليموالدريب","子类别":〔{"categoryId":1,"name":والدريب","id":1},{"categoryId":1,"name":والتدريب","id":2}],"id":1}]、"targetUrl":null、"success":true、"error":null,"unAuthorizedRequest":false、"__abp":true}
let dictionary = try? JSONSerialization.jsonObject(with: string, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any]