Rodrigo Schreiner Rodrigo Schreiner - 3 months ago 23
JSON Question

Parsing Json not displaying all items in it

I have been trying to make this code work with swift3 latest beta version but got tired of no luck

Can you please someone try tell me how to fix it. I have 3 items that come back on my response but I can just see one been displayed on my tableview below the code

that is my response


(
{
itemaddedby = Rodrigo;
itemaddeddate = "2016-08-24 17:37:07";
itemdescription = pizza;
itemid = 66;
},
{
itemaddedby = "";
itemaddeddate = "2016-08-26 22:38:33";
itemdescription = "";
itemid = 67;
},
{
itemaddedby = rodrigo;
itemaddeddate = "2016-08-23 22:45:51";
itemdescription = cheese;
itemid = 65;
} )


func GetAllItems(){
var processedOk = true
if let url = URL(string: "http://www.goemobile.com/mobile/todo/getitems.php"){
let request = NSMutableURLRequest(url:url)
request.httpMethod = "POST";// Compose a query string
let postString = ""
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("1\(error)")
}
else{
_ = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
}
do {


if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray {
if convertedJsonIntoDict.count > 0{
let item = (convertedJsonIntoDict[0] as! NSDictionary)["itemdescription"] as? String
if item != nil{
self.TableData.append(item!)
DispatchQueue.global().async {
DispatchQueue.main.async {
self.table.reloadData()
}
}
}
else{

// self.lblError.text = "Something went wrong. Try again"
}
}
else{
processedOk=false
}
}
else{
DispatchQueue.global().async {
DispatchQueue.main.async {

}
}
}
}
catch let error as NSError {
print(error.localizedDescription)
}

}
task.resume()
self.table.reloadData()
}

}


Thanks a lot for the help

Answer

Try this, unfortunately not tested

func getAllItems(){ // function names are supposed to start with a lowercase letter
  if let url = URL(string: "http://www.goemobile.com/mobile/todo/getitems.php"){
    var request = URLRequest(url:url)
    request.httpMethod = "POST";// Compose a query string
    let postString = ""
    request.httpBody = postString.data(using: String.Encoding.utf8)
    let task = URLSession.shared.dataTask(with:request) { data, response, error in

      if error != nil{
        print("1\(error)")
        return
      }
      do {
        if let convertedJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [[String:Any]] {
          for item in convertedJson {
            if let description =  item["itemdescription"] as? String {
              self.TableData.append(description)
            }
          }
          DispatchQueue.main.async {
            self.table.reloadData()
          }
        }
      }
      catch let error as NSError {
        print(error.localizedDescription)
      }
    }
    task.resume()
  }
}

The code uses Swift native collection types rather than Foundation NSArray / NSDictionary. It makes things a lot easier.