如何将JSON代码解析为Swift 3 Table View数据?

时间:2017-03-11 15:12:38

标签: swift xcode swift3

我正在使用swift 3和MySQL以及PHP作为数据库创建一个ios应用程序。我想将数据从数据库输出到tableview,但我一直有错误:Error Domain = NSCocoaErrorDomain Code = 3840“JSON文本没有以数组或对象开头,并且选项允许未设置片段。”我尝试在PHP中检查JSON编码,这没关系。

这是我的快速代码:

class Roster: UITableViewController {

@IBOutlet weak var rosterbar: UITabBarItem!
var class_id = String()
 var values: Array! = []

override func viewDidLoad() {

}

func SelectClassName(){
    let request = NSMutableURLRequest(url: NSURL(string: "http://localhost/classdbfiles/SelectClass.php")! as URL)
    request.httpMethod = "POST"
    let postString = "class_id=\(Home.ClassVariables.class_id)"
    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("error=\(error)")
            return

        }
        print("response=\(response!)")
        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("reponseString = \(responseString!)")


    }
    task.resume()

    }



func get() {
    guard let url = URL(string: "http://localhost/classdbfiles/SelectClass.php") else { return }

    do {
        let data = try Data(contentsOf: url)
        let deserializedValues = try JSONSerialization.jsonObject(with: data)
        guard let arrayOfDictionaryValues = deserializedValues as? [[String:String]] else { return }
        values = arrayOfDictionaryValues

    } catch {
        //You should separate the different catch blocks to handle the different types of errors that occur
        print("There was an error:\(error)")
    }
    tableView.reloadData()
}

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count
}

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SpecialCell
    let maindata = values[indexPath.row] as! [String:AnyObject]

    cell.studentname.text = maindata["lastname"] as? String

    return cell
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    SelectClassName()
    get()
    tableView.reloadData()
}

}

这是PHP文件:

if ($result = $mysqli->query("SELECT lastname from tbl_studentinfo where class_id='".$class_id."'")) {
    $tempArray = array();
    while($row = $result->fetch_object()) {
            $tempArray = $row;
            array_push($myArray, $tempArray);
        }
    echo json_encode($myArray);
    }
$result->close();
$mysqli->close();

1 个答案:

答案 0 :(得分:1)

假设您收到了以下格式的JSON文件:

 {
    "members": [
        {"name": "Sarah"},
        {"name": "David"},
        {"name": "Michael"}
    ]
}

(请注意,花括号表示字典,其中方括号表示数组。)

导入捆绑包中的JSON文件。我打电话给我的jsonfile.json

然后使用以下方法解析JSON文件并获取成员,即包含成员名称的字典:

func getMemeberDictionary()-> [[String: String]]{
        guard let jsonFileURL = Bundle.main.url(forResource: "jsonfile", withExtension: "json") else {fatalError("Failed to get JSON file URl")}
        var jsonData: Data!
        var jsonDictionary: [String: AnyObject]!

        //Getting JSON data from URL
        do {
            jsonData = try Data(contentsOf: jsonFileURL)
        } catch let error as NSError{
            print(error.debugDescription)
            fatalError("Failed to initiate JSON data from URL!")
        }

        //getting top level JSON dictionary i.e. "members"
        do {
            jsonDictionary = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: AnyObject]
        } catch let error as NSError{
            print(error.debugDescription)
            fatalError("Failed to initiate top level JSON dictionary")
        }

        let memebers: [[String: String]] = jsonDictionary["members"] as! [[String: String]]

        for person in memebers{
            print(person["name"]!)
        }

        return memebers
    } 

这将返回一个[[String:String]]类型的数组,用它来设置表视图。

此代码将所有成员导入表视图:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var members: [[String: String]]!

    override func viewDidLoad() {
        super.viewDidLoad()
        members = getMemeberDictionary()
    }

    func getMemeberDictionary()-> [[String: String]]{
        guard let jsonFileURL = Bundle.main.url(forResource: "jsonfile", withExtension: "json") else {fatalError("Failed to get JSON file URl")}
        var jsonData: Data!
        var jsonDictionary: [String: AnyObject]!

        //Getting JSON data from URL
        do {
            jsonData = try Data(contentsOf: jsonFileURL)
        } catch let error as NSError{
            print(error.debugDescription)
            fatalError("Failed to initiate JSON data from URL!")
        }

        //getting top level JSON dictionary i.e. "members"
        do {
            jsonDictionary = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: AnyObject]
        } catch let error as NSError{
            print(error.debugDescription)
            fatalError("Failed to initiate top level JSON dictionary")
        }

        let memebers: [[String: String]] = jsonDictionary["members"] as! [[String: String]]

        for person in memebers{
            print(person["name"]!)
        }

        return memebers
    }
}


extension ViewController: UITableViewDelegate, UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return members.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            cell.textLabel?.text = members[indexPath.row]["name"]
            return cell
        } else {
            cell?.textLabel?.text = members[indexPath.row]["name"]
            return cell!
        }
    } 
}

结果:

enter image description here

如果您有任何其他问题,请随时向我提出。