Ios 我想将字符串转换为字典swift 4
如何在swift中将Ios 我想将字符串转换为字典swift 4,ios,iphone,ipad,alamofire,swift4,Ios,Iphone,Ipad,Alamofire,Swift4,如何在swift中将字符串转换为字典 我的代码 Alamofire.request(apiMethod).responseJSON { response in let statCode = response.response?.statusCode print("URLRequest: \(String(describing: response.request!))") print("RequestSuccess: \(String(describ
字符串
转换为字典
我的代码
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是我的字符串
我需要更改为字典
:
{“结果”:[{“类别”:2,“名称”:“测试”,“子类别”:[],“id”:2},{“类别类型”:1,“名称”:“测试”
“子类别”:[{“categoryId”:1,“名称”:“categoryName”:“类别名称”:“categoryName”:”
“id”:1,{“categoryId”:1,“name”:“categoryName”:“categoryName”:“categoryName”:“categoryName”:“categoryName”:“categoryName”:“categoryName”:
“id”:2}],“id”:1}],“targetUrl”:null,“success”:true,“error”:null,“unAuthorizedRequest”:false,“u abp”:true}”
其他打印输出是什么?在使用Alamofire时,由于使用了
.responseJSON{}
,您应该已经将一个序列化的JSON放入字典中。如果让JSON=response.result.value{print(“JSON:\(JSON)”)}
-在这个块中查找json。
let dictionary = try? JSONSerialization.jsonObject(with: string, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any]