Essentially I would like to parse the JSON received from a url and display the data inside of alert popup.
I am able to do this with a simple array below but don't know how this can be done with an JSON array from a URL.
Currently working:
let jsonObj:[String: Any] = ["error": [ "email": ["The email has already been taken."], "phone": ["The phone has already been taken."]] ] if let errorMsgs = jsonObj["error"] as? [String: [String]] { let errMsg = errorMsgs.values.map { $0.reduce("", +) }.joined(separator: "\n") print(errMsg) self.presentAlert(withTitle: "Try again", message: errMsg) } JSON Array:
var structure = [Struct]() private func fetchJSON() { guard let url = URL(string: "url.com") else { return } var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = "code=\(codeValue)".data(using: .utf8) URLSession.shared.dataTask(with: request) { data, _, error in guard let data = data else { return } do { self.structure = try JSONDecoder().decode([Struct].self,from:data) } catch { print(error) } }.resume() } struct Struct: Decodable { let id: Int let reasons: String } Sample JSON Array:
[ { "id": 1, "reasons": "Test" }, { "id": 3, "reasons": "Test" } ] How can I get the reasons string from the array to populate in the alert similar to above.
https://stackoverflow.com/questions/66558079/issue-displaying-array-data-inside-uialert-view March 10, 2021 at 11:50AM
没有评论:
发表评论