swift无法解析php数据

时间:2017-07-25 10:14:23

标签: php ios swift3

我一直在尝试通过postString从数据库中获取某些值,但它不会返回任何值。 Php经过测试,工作正常,任何帮助将不胜感激。以下是代码。

import UIKit

class DisplayDetailedViewController: UIViewController {

    var role = String()
    var date = String()
    var city = String()
    var mark_ref = String()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func getData(){
        let myUrl = URL(string:"http://xxxxxx.co.in/xxxxx/show_report1.php")

        var request = URLRequest(url: myUrl! as URL)
        request.httpMethod = "POST"

        let postString = "city=\(city)&date=\(date)&market_rep=\(mark_ref)"
        request.httpBody = postString.data(using: String.Encoding.utf8)

        let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
            DispatchQueue.main.async{
                    if error != nil{
                        print("error is\(String(describing: error))")
                    }
                    do{
                        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                        if let parseJSON = json {

                            let myBoard : String!
                            myBoard = parseJSON["success"] as! String?

                            print(myBoard)
                        }
                    }
                    catch {
                        print(error)
                    }
            } 
        }
        task.resume()   
    }
}

1 个答案:

答案 0 :(得分:0)

尝试1:替换下面的行(NSDictionary)

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray

尝试2:替换下面的行(NSArray)

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray

尝试3:替换下面的行(NSString)

{{1}}

还尝试选项:.mutableContainers - > options:JSONSerialization.ReadingOptions.allowFragments