2017-09-13 69 views
1

在郵遞員我的JSON響應看起來像這樣解析JSON ...問題用Alamofire

{ 
    "success": 1, 
    "Details": { 
     "seller_id": "165", 
     "mobile_no": "9653265987", 
     "seller_name": "User A", 
     "seller_email_id": "[email protected]", 
     "company_name": "myCompany", 
     "category": "Cosmetics", 
     "otp": "1111" 
    }, 
    "message": "Seller Already Registered!!!" 
} 

現在我想給基於成功是0還是1和條件所以,我想提取成功。我也想提取手機號碼。但我無法弄清楚如何做到這一點。

這是我想提出我的Alamofire職務的請求......

Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers) 
      .responseString { (response) in 

       if let httpResponse = response.response { 
        print("error \(httpResponse.statusCode)") 

        if httpResponse.statusCode == 200 { 
         //Do something 

        } 
       } 
     } 

我沒有去通過其他用戶提出類似的問題...但無法從他們身上找到了我的問題的精確解...

+0

您使用任何第三方用於像swiftyjson或光澤數據的序列化..? –

+0

No..just Alamofire .. –

回答

0

有幾個問題:

  1. 您的參數到request看起來不太正確。

    • parameters參數看起來不正確的:在最好的,這是很奇怪有Parameters變量(你應該使用變量小寫;並與Alamofire類型相沖突);編碼應爲URLEncoding.default。或者,因爲URLEncoding.default的默認值,您可以完全忽略它。

  2. 如果您要檢查狀態代碼,讓validate爲你做的。

  3. 如果您期待JSON作爲迴應,請使用reponseJSON而不是responseString

  4. 使用if letguard letas?安全地打開可選項。不要使用as!(除非您100%確定它永遠不會失敗......並且在處理來自遠程服務器的響應時,您絕不會100%自信)。

這樣:

func performRequest(url: String, parameters: [String: String]?, headers: [String: String]?) { 
    Alamofire.request(url, method: .post, parameters: parameters, headers: headers) 
     .validate(statusCode: 200 ..< 300) 
     .responseJSON { response in 
      switch response.result { 
      case .success(let value): 
       guard let dictionary = value as? [String: Any], 
        let success = dictionary["success"] as? Int else { 
         print("success not found") 
         return 
       } 

       guard success == 1 else { 
        print("web service reported failure") 
        return 
       } 

       guard let details = dictionary["Details"] as? [String: Any] else { 
        print("Did not find details") 
        return 
       } 

       guard let mobile = details["mobile_no"] as? String else { 
        print("mobile not found") 
        return 
       } 

       print(mobile) 

      case .failure(let error): 
       print("Failed: \(error)") 
      } 
    } 
} 

或是在下面的評論,如果你想goToSignUpScreen()如果success0,則:

Alamofire.request(url, method: .post, parameters: parameters, headers: headers) 
    .validate(statusCode: 200 ..< 300) 
    .responseJSON { response in 
     switch response.result { 
     case .success(let value): 
      guard let dictionary = value as? [String: Any], 
       let success = dictionary["success"] as? Int else { 
        print("success not found") 
        return 
      } 

      if success == 0 { 
       self.goToSignUpScreen() 
      } 
     case .failure(let error): 
      print("Failed: \(error)") 
     } 
} 
+0

該解決方案適用於@Rob。但我有一個疑問..它很簡單,肯定... :)但請考慮它是從初學者的東西。懷疑是這個..你提到這條線讓成功=字典[「成功」]爲?詮釋其他{...現在在這裏,如果我想給這個...如果讓結果= response.result.value爲? [String:Any] { if result [「success」] as! Int == 0 { self.gotToSignUpScreen() } 那麼我怎麼在guard語句中提到它呢? –

+0

首先,爲什麼要使用'response.result.value'而不是使用'.success(let value)'提供的'value'?其次,爲什麼使用'as!'而不是'as?'?如果轉換失敗(如果你知道它不會發生),那麼只有在你的應用程序崩潰時才使用'!'。但是如果你想'goToSignUpScreen'如果'成功'爲零,你當然可以做到這一點,如上所示。 – Rob

+0

感謝@Rob澄清... –

2

試試這個

Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers) 
      .responseJSON { (response) in 

       switch response.result{ 
       case .success(_): 
        print("Success: \(response)") 
        let JsonResponse = response.result.value as? [String: Any] 

        if JsonResponse?["success"] as! Int == 1{ 
         let dict = JsonResponse?["Details"] as! [String : Any] 
         let mobileNo = dict["mobile_no"] as! String 
        } 
        else{ 
         print("Failed") 
        } 

       case .failure(let error): 
        print("Failed: \(error)") 
       } 
     } 
+0

在這一行讓JsonResponse = response.result.value as? [字符串:任何],我收到警告''從字符串轉換?到無關的類型[字符串:任何]總是失敗''..也許正因爲如此,在接下來的一行,它崩潰... –

+0

好吧,... @羅布...所以什麼可以是一個適當的修復在這種情況下... –

+0

@ User.bw嘗試我更新的代碼。 –