2016-11-22 35 views
0

我在XCode 8中以不同格式生成JSON響應,因爲它用於出現在XCode 7.3中如何在swift 3.0中獲得Json響應?

因此,我的應用程序停止響應。我正在分享我的回覆的截圖。

這是我使用

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 } 
+1

告訴我們,您使用的代碼。 –

+0

另外顯示舊的和新的JSON響應。並將其作爲文本複製並粘貼到問題中(而不是使用屏幕截圖)。 – Codo

+0

由於xcode更新和時間短缺,無法顯示舊響應。 –

回答

0

試試這個: -

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)") 


      } 

     } 
+0

我必須按照我在問題中陳述的方式來執行此操作,當我們擊中API並且我們正在獲得響應時,您的方式將起作用。 –

+0

如上所述,您可以使用「dataDict」,而不是「userInfo」,這對我有用 –