快速解析json数据时出现错误,“由于数据格式不正确,无法读取数据。” [英] error when parsing json data with swift "The data couldn’t be read because it isn’t in the correct format."

查看:1020
本文介绍了快速解析json数据时出现错误,“由于数据格式不正确,无法读取数据。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道从数据库中检索数据并使用它来检查用户是否退出(登录)的任何人,请帮忙。我头疼,不幸的是我的编码还不足以解决问题!

anyone who knows about retrieving data from a database and using it to check if a user exits (sign in) please help. My head is wrecked and unfortunately my coding is not quite good enough to fix the problem!

 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]作为吗?字符串

I think the issue is with the if let json = try line, the data is not in the correct format. My php script is working well and I can pass parameters in the browser and all seems ok. The key is correct (userId). I think another issue is with let userID = parseJSON["userId"] as? String

如果我已将数据作为NSDictionary进行了请求-如何将该信息转换为字符串类型(我确定您仍然无法这样做) -很多问题。我需要在另一个视图控制器中使用String表示形式。我已经尝试了很长时间了,但是我无法使它正常工作,所以我将非常感谢您的帮助-
在我运行代码时-它绕过了 do块,直接进入.localizedDescription

if I have made a request for the data as an NSDictionary - how can I convert this info to type String (I'm sure you can't anyways) Sorry - a lot of questions. I need String representations to use in another view controller. I have been trying this for ages and I can't get it to work so I would really appreciate some help- When i run the code- it bypasses the "do" block and goes straight to the .localizedDescription

感谢您抽出宝贵时间阅读我的v long问题!

Thank you for talking the time to read my v long question!

推荐答案

问题肯定出在NSJSONSerialization.JSONObjectWithData,由于某种原因它不喜欢您的JSON。

The issue is definitely in NSJSONSerialization.JSONObjectWithData which doesn't like your JSON for some reason.

在将其输入NSJSONSerialization之前尝试清理JSON:

Try cleaning your JSON before feeding it to NSJSONSerialization:

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

这篇关于快速解析json数据时出现错误,“由于数据格式不正确,无法读取数据。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆