2016-08-13 182 views
1

我是Swift上的新成員,但我正在回顧一些例子,例如如何使登錄應用程序發送用戶名和密碼字段到PHP文件並獲取json響應。Swift 3 problems解析json響應

當我打印我的responseString我得到:

[{ 「用戶id」:1, 「用戶名」: 「羅德里戈」, 「密碼」: 「minhoca」, 「組名」: 「情侶」}]

但是當我嘗試解析JSON我從來沒有可以設置用戶名變量,因爲從來沒有進入的那部分代碼,我只是得到「這裏」

感謝您的幫助

func sendLoginInfo(username: String, password: String) -> String{ 
     if let url = URL(string: "myphpurl"){ 
      let request = NSMutableURLRequest(url:url) 
      request.httpMethod = "POST";// Compose a query string 
      let postString = "?username=\(myUsername)&password=\(myPassword)" 
      request.httpBody = postString.data(using: String.Encoding.utf8) 
      let task = URLSession.shared.dataTask(with:request as URLRequest){ 
       data, response, error in 

       if error != nil{ 
        print("1\(error)") 
       } 
       else{ 
        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
        print("response string = \(responseString!)") 
       } 

       do { 

        if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary { 

         // Print out dictionary 
         print(convertedJsonIntoDict) 

         // Get value by key 
         let firstNameValue = convertedJsonIntoDict["username"] as? String 
         print("here = \(firstNameValue!)") 

        } 
        else{ 
         print("here") 
        } 
       } catch let error as NSError { 
        print(error.localizedDescription) 
       } 
      } 
      task.resume() 
     } 
     return "" 
    } 

回答

3

變化將NSDictionary轉換爲NSArray在你的代碼,因爲你得到一個數組,並試圖轉換到dictonary:

if let convertedJson = try JSONSerialization.jsonObject(with: data!, options: []) as? NSArray 

獲得索引0處的對象,這將給你dictonary &那麼你可以得到的用戶名

所以最終代碼會:

func sendLoginInfo(username: String, password: String) -> String{ 
    if let url = URL(string: "myphpurl"){ 
     let request = NSMutableURLRequest(url:url) 
     request.httpMethod = "POST";// Compose a query string 
     let postString = "?username=\(myUsername)&password=\(myPassword)" 
     request.httpBody = postString.data(using: String.Encoding.utf8) 
     let task = URLSession.shared.dataTask(with:request as URLRequest){ 
      data, response, error in 

      if error != nil{ 
       print("1\(error)") 
      } 
      else{ 
       let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
       print("response string = \(responseString!)") 
      } 

      do { 

       if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSArray { 

        // Print out dictionary 
        print(convertedJsonIntoDict) 

        // Get value by key 
        let firstNameValue = (convertedJsonIntoDict[0] as! NSDictionary)["username"] as? String 
        print("here = \(firstNameValue!)") 

       } 
       else{ 
        print("here") 
       } 
      } catch let error as NSError { 
       print(error.localizedDescription) 
      } 
     } 
     task.resume() 
    } 
    return "" 
} 
+0

謝謝,但現在它給了我: 使用未申報類型的NSDictionary –

+0

對不起我的代碼做了一個錯字。再次嘗試此代碼我已編輯它。我拼寫關鍵字它是NSDictionary –

+0

非常感謝,這工作得很好。 –