I am creating an ios application using swift 3 and MySQL and PHP as database. I wanted to output the data from database to a tableview but i kept having an error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." I tried to check the JSON encoding in PHP, and it was ok.
Here's my swift code:
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()
}
}
and here's the PHP file:
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();
Lets say you received a JSON file of the form:
(Note that curly brackets denote a dictionary, where as square brackets an array.)
Import the JSON file in your bundle. I called mine jsonfile.json
Then use the following method to parse the JSON file and get the members i.e. a dictionary that contains the member names:
This will return an array of type [[String: String]], use it to set up your table view.
This code imports all members into a table view:
Result:
Don't hesitate to ask me any further questions you may have.