![](/img/trans.png)
[英]How to Convert Json response to Swift 3.0, older conversion is throwing Error?
[英]How to get Json Response in swift 3.0?
我在XCode 8中得到了不同格式的JSON响应,因为它曾经出现在XCode 7.3中
因此,我的应用停止了响应。 我正在分享我的回复的屏幕截图。
这是我正在使用的代码
func MobileNumber(_ notification : Notification){
activityIndicator.stopAnimating()
if let notificationDict = (notification as NSNotification).userInfo{
print(notificationDict)
if let dataDict = notificationDict["Data"] as? NSDictionary{
print(dataDict)
if let status = dataDict["response"] as? Int{
switch status{
case 1:
if let data = dataDict["data"] as? NSDictionary{
if let mobileNumber = data["phone"] as? String{
UserDefaults.standard.set(mobileNumber, forKey: DefaultsKey.phoneNumber.rawValue)
print(mobileNumber)
}
UpdateFrame1()
}
JSON响应:
Data]: 243 bytes
[Result]: SUCCESS: {
data = {
"__v" = 0;
"_id" = 5833e131de2fe2342b0d0410;
code = "<null>";
"created_at" = "2016-11-22T06:09:53.097Z";
"expiry_date" = "2016-11-22T06:09:53.097Z";
phone = 9********;
status = active;
};
message = "Resending your unused auth code";
response = 1;
}
[Timeline]: Timeline: { "Request Start Time": 501493300.746, "Initial Response Time": 501493302.862, "Request Completed Time": 501493302.868, "Serialization Completed Time": 501493302.872, "Latency": 2.116 secs, "Request Duration": 2.122 secs, "Serialization Duration": 0.003 secs, "Total Duration": 2.126 secs }
尝试这个:-
let val = userInfo["SUCSESS"]
if let jsonData = (val as AnyObject).data(using: String.Encoding.utf8.rawValue)
{
do{
let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers )
if let dict = jsonObject as? [String: AnyObject]
{
print("YOUR PHONE NUM \((dict as? [String : AnyObject])?["data"]?["phone"])")
}
}catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.