Serializing JSON with Swift 3

I am trying to populate a table by populating an array of JSON data. I use the code below, but keep getting the error:

The type "Any" has no substring elements

in the following lines of code:

self.tableData.append(jsonResult[i]["title"]! as! String)
self.tableImages.append(jsonResult[i]["image"]! as! String)
self.tableDesc.append(jsonResult[i]["description"]! as! String)
self.tableValidity.append(jsonResult[i]["validity"]! as! String)

My code is:

 let str3 = Int(str2!)!
            let url = NSURL(string: "https://www.*****.php")!

            let task = URLSession.shared.dataTask(with: url as URL) { (data, response, error) -> Void in
                if let urlContent = data {
                    do {

                        let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)


                        print(str3)

                        var i = 0

                        while i < str3 {
                            print(jsonResult[i]["title"]! as!String)
                            print(jsonResult[i]["image"]! as! String)

                            self.tableData.append(jsonResult[i]["title"]! as! String)
                            self.tableImages.append(jsonResult[i]["image"]! as! String)
                            self.tableDesc.append(jsonResult[i]["description"]! as! String)
                            self.tableValidity.append(jsonResult[i]["validity"]! as! String)

                            i = i + 1

                        }

                    } catch {
                        print("JSON serialization failed")
                    }                    

                } else {                    
                    print("ERROR FOUND HERE")
                }                

                DispatchQueue.main.async(execute: { () -> Void in

                    self.tableView.reloadData()

                })                                

            }

            task.resume()
+4
source share
2 answers

The compiler does not know the type jsonResult, you have to say what it is, for example, with optional binding, for example:

if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [[String:AnyObject]] {


}

Here I omit JSON as an array of dictionaries. Use this loop inside this if letand it should work.

+4
source

The compiler does not know the type of the JSON object, you need to give it to the actual type

let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options:[]) as! [[String:Any]]

Compatible containers are not needed at all.

C- while . Swift.

for item in jsonResult {

  print(item["title"] as! String)
  print(item["image"] as! String)

  self.tableData.append(item["title"] as! String)
  self.tableImages.append(item["image"] as! String)
  self.tableDesc.append(item["description"] as! String)
  self.tableValidity.append(item["validity"] as! String)

}

PS: . -

+4

Source: https://habr.com/ru/post/1654586/


All Articles