解析Json不显示其中的所有项目

时间:2016-08-27 20:37:39

标签: json nsdictionary

我一直在努力使这个代码与swift3最新测试版一起工作,但厌倦了没有运气

有人可以试着告诉我如何解决它。我有3个项目回复我的回复,但我可以看到一个项目已经显示在我的桌面视图下面

这是我的回复

  

(           {           itemaddedby =罗德里戈;           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()
        }

    }

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

试试这个,遗憾的是没有经过测试

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()
  }
}

代码使用Swift本机集合类型而不是Foundation NSArray / NSDictionary。它使事情变得容易多了。