2016-07-13 3 views
1

誰(サインイン)助けてください。私の頭は壊れてしまい、残念ながら私のコーディングは問題を解決するのに十分ではありません!swsonでjsonデータを解析するときにエラーが発生しました。「正しい形式でないため、データを読み取れませんでした。データベースからデータを取得し、ユーザー出口かどうかを確認するためにそれを使用して知っている

do { 


       if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary 
       { 


        let parseJSON = json 

        let userID = parseJSON["userId"] as? String //dictionary object that uses a key 

        if(userID != nil) 
        { 
         //everything is fine and take user to main page 
         //instantiate the view controller 
         let profilePage = self.storyboard?.instantiateViewControllerWithIdentifier("ProfilePageViewController") as! ProfilePageViewController 

         //wrap it in nav controller 

         let profilePageNav = UINavigationController(rootViewController: profilePage) 

         //take user to this page 
         //window root view controller- existing is replaced and no back button 

         let appDelegate = UIApplication.sharedApplication().delegate 
         appDelegate?.window??.rootViewController = profilePageNav 




        }else{ 
         //display the alert message from the php file 
         let errorMessage = parseJSON["message"] as? String 


         let myAlert = UIAlertController(
          title: "Alert!", 
          message: errorMessage, 
          preferredStyle: UIAlertControllerStyle.Alert) 

         //create a button action - 
         let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) 

         //adding ok button and presenting to a user and once ok button is pressed- dismiss the view 
         myAlert.addAction(okAction) 
         self.presentViewController(myAlert, animated: true, completion:nil) 





        } 


       }//end of if let json 
      }//end of do 
      catch let error as NSError { 
       print(error.localizedDescription) 
      } 



      print("success") 

     }//end of dispatch_async 
    }).resume()//end of request 




}//end of action sign in 

私はこの問題がif json = try回線の場合、データが正しい形式ではないと思います。私のPHPスクリプトはうまくいきました。私はブラウザでパラメータを渡すことができます。キーは正しいです(userId)。 別の問題はlet userID = parseJSON ["userId"]としていると思いますか?文字列

私はNSDictionaryのようにデータの要求をした場合 - どのように私は申し訳ありませんが(私はあなたがいないとにかくことができると確信してい)この情報は、文字列を入力する変換することができます - 多くの質問。私は別のビューコントローラで使用する文字列表現が必要です。私は年齢のためにこれをしようとしていると私はそれが仕事を得ることができないので、私はコード - を実行すると、私はいくつかのヘルプ - を本当に感謝それが「やる」ブロックを迂回し.localizedDescription

感謝し直進しますあなたは私の長い質問を読む時間を話してくれた!

+1

APIから返されるJSON文字列は何ですか? –

+0

文字列は、INT-は、この問題を引き起こしている可能性があるのでデータベース - のuser_idが自動インクリメントし、その値として設定されているphpMyAdminのからのuserIdを返していますか?それはのuserIdがINT-は、varchar型の値を解析しようとしたとなっているという事実とは何の関係もありませんquestion-私は数のユーザーに自動インクリメントにあり、固有の識別子 – user6573720

+0

として電子メールを持っているNO-私は自分自身に答えました同じエラー – user6573720

答えて

0

問題は、何らかの理由であなたのJSONを好まないNSJSONSerialization.JSONObjectWithDataに間違いです。

はNSJSONSerializationにそれを供給する前に、あなたのJSONをクリーニングしてみてください:

var dataString = String(data: data, encoding: NSUTF8StringEncoding)! 
dataString = dataString.stringByReplacingOccurrencesOfString("\\'", withString: "'") 
let cleanData: NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)! 
0

要求 "request.setValue送信中に(" アプリケーション/ JSONを "forHTTPHeaderField: "同意する")" 私のエラーを解決しました。

関連する問題