Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用swift分析json数据时出错;数据无法’;因为它是’;t的格式正确。”;_Php_Json_Database_Swift - Fatal编程技术网

Php 使用swift分析json数据时出错;数据无法’;因为它是’;t的格式正确。”;

Php 使用swift分析json数据时出错;数据无法’;因为它是’;t的格式正确。”;,php,json,database,swift,Php,Json,Database,Swift,任何了解从数据库检索数据并使用它检查用户是否退出(登录)的人,请提供帮助。我的脑袋坏了,不幸的是,我的编码不够好,无法解决这个问题 do { if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary { let parseJSON = 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 let json=try行,数据的格式不正确。我的php脚本运行良好,我可以在浏览器中传递参数,一切似乎都正常。密钥是正确的(userId)。 我认为另一个问题是让userID=parseJSON[“userID”]作为?串

如果我请求将数据作为NSDictionary-如何将此信息转换为String类型(我肯定你无论如何都不能)对不起-有很多问题。我需要在另一个视图控制器中使用字符串表示。我已经尝试了很长时间,但我无法让它工作,所以我非常感谢您的帮助- 当我运行代码时,它绕过“do”块,直接进入.localizedDescription


感谢您在聊天的时候阅读我的v长问题

问题肯定出在NSJSONSerialization.JSONObjectWithData中,因为某些原因,它不喜欢您的JSON

在将JSON馈送到NSJSONSerialization之前,请尝试清理JSON:

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

发送请求“request.setValue(“application/json”,forHTTPHeaderField:“Accept”)”时解决了我的错误。

从API返回的json字符串是什么?该字符串从phpmyadmin数据库返回userId-user_id设置为自动递增,其值设置为Int-这是否会导致问题?我必须自动增加用户数量,并将电子邮件作为唯一标识。否-我已经回答了我自己的问题-这与userId是Int这一事实无关-试图解析varchar值,但得到了相同的错误