2016-08-13 19 views
1

私はSwiftの新機能ですが、ログインアプリがユーザー名とパスワードのフィールドをphpファイルに送信し、json応答を返すような例をいくつか挙げました。Swift 3の問題jsonの応答を解析する

私は私のresponseStringを印刷するとき、私は得る:

[{ "USERID":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 

あなたがdictonary &を与えるどのインデックス0でオブジェクトを取得、その後、あなたは最終的なコードがされるユーザ名

を得ることができます:

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

おかげだが、今では私を与える: –

+0

申し訳ありませんが、私はコードのタイプミスをしたNSDictionaryの宣言されていないタイプの 使用。このコードをもう一度試してください私はそれを編集しました。私はそれがNSDictionaryであることを間違って綴られたキーワード –

+0

ありがとう、それは正常に働いた。 –