Display information from decoded JSON in a UILabel (Swift 4) - ios

I am trying to display information taken from JSON. I've used .decode to get it. Now I want to put its text onto a simple label on my storyboard. At the bottom under ".resume()" is my attempt and it isn't working. I can't seem to figure this out.
import UIKit
struct WebsiteDescription: Decodable {
var name : String
var description : String
var courses : [Course]
}
struct Course: Decodable {
let id: Int
let name: String
let link: String
let imageUrl: String
}
class ViewController: UIViewController {
#IBOutlet weak var displayLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let jsonUrlString = "https://api.letsbuildthatapp.com/jsondecodable/website_description"
guard let url = URL(string: jsonUrlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do {
let websiteDescription = try JSONDecoder().decode(WebsiteDescription.self, from: data)
print(websiteDescription.name, websiteDescription.description, websiteDescription.courses)
//let courses = try JSONDecoder().decode([Course].self, from: data)
} catch let jsonErr {
print("Error serializing json", jsonErr)
}
}.resume()
let displayLabel.text = websiteDescription.name
}
}

You need to update your label from within the do method. Also you should do it from the main thread so that the UI can be updated. I have updated the code below.
import UIKit
struct WebsiteDescription: Decodable {
var name : String
var description : String
var courses : [Course]
}
struct Course: Decodable {
let id: Int
let name: String
let link: String
let imageUrl: String
}
class ViewController: UIViewController {
#IBOutlet weak var displayLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let jsonUrlString = "https://api.letsbuildthatapp.com/jsondecodable/website_description"
guard let url = URL(string: jsonUrlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do {
let websiteDescription = try JSONDecoder().decode(WebsiteDescription.self, from: data)
print(websiteDescription.name, websiteDescription.description, websiteDescription.courses)
//let courses = try JSONDecoder().decode([Course].self, from: data)
DispatchQueue.main.async {
self.displayLabel.text = websiteDescription.name
}
} catch let jsonErr {
print("Error serializing json", jsonErr)
}
}.resume()
}

Related

How to decode and get values from API response that is stored in a dictionary in swift

I have an API response that is stored in a dictionary, I have created structs in a different folders but when I try to decode them using decode method it throws an error, the error mentions that "The data couldn’t be read because it isn’t in the correct format." , here is the code and structs I have built:
import UIKit
class ViewController: UIViewController {
#IBOutlet var cityButtons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func handleSelection(_ sender: UIButton) {
cityButtons.forEach{ (button) in
UIView.animate(withDuration: 0.3) {
button.isHidden = !button.isHidden
self.view.layoutIfNeeded()
}
}
}
enum Cities:String {
case amman = "Amman"
case azzerqa = "Az zerqa"
case irbid = "Irbid"
case aqaba = "Aqaba"
}
#IBAction func cityTapped(_ sender: UIButton) {
guard let title = sender.currentTitle, let City = Cities(rawValue: title)
else {
return
}
var city:String
switch City {
case .amman:
city = "Amman"
case .azzerqa:
city = "zerqa"
case .irbid:
city = "Irbid"
case .aqaba:
city = "Aqaba"
}
let url = URL(string: "https://api.weatherapi.com/v1/current.json?key={ket}&q=\(city)")
guard url != nil else {
print("error creating URL Object")
return
}
var request = URLRequest(url: url!, cachePolicy: .useProtocolCachePolicy , timeoutInterval: 10)
let headers = ["Content-Type" : "application/json"]
request.allHTTPHeaderFields = headers
request.httpMethod = "GET"
let session = URLSession.shared
let dataTask = session.dataTask(with: request) {(data, response, error) in
if error == nil && data != nil {
do {
let dictionary = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [String:Any]
let decoder = JSONDecoder()
print(dictionary)
do {
let weatherdatadecoded = try decoder.decode(WeatherData.self, from: data!)
print(weatherdatadecoded)
}
catch {
print(error.localizedDescription)
}
}
catch {
print(error.localizedDescription)
}
}
}
dataTask.resume()
}
}
and here are the structs (each one is in a separate file):
import Foundation
struct WeatherData : Codable {
var location: Location?
var current: Current?
}
import Foundation
struct Location : Codable {
var name: String = ""
var region: String = ""
var localtime: String = ""
var country: String = ""
}
import Foundation
struct Current : Codable {
var temp_c = 0.0
var is_day = false
var condition: Condition?
}
import Foundation
struct Condition : Codable {
var text: String = ""
var icon: String = ""
var code: Int = 0
}

JSON throws nil on every request

First time I'm trying to parse JSON data. The output comes out -
Course (success: zero, timestamp: nil, base: nil, date: nil, courses: nil)
Why is there nil everywhere?
I tried to change the value in the "Course" and "Currency" structures but did not lead to success
import UIKit
import JavaScriptCore
struct Course: Decodable {
var success: Bool?
var timestamp: Int?
var base: String?
var date: String?
var rates: Currency?
}
struct Currency: Decodable {
var USD: Float
var AUD: Double
var CAD: Double
var PLN: Double
var MXN: Double
}
class JsonViewContoller: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let urlData: String = "http://data.fixer.io/api/latest?access_key=7ac2982c82da926b787fd2f089b110e5&symbols=USD,AUD,CAD,PLN,MXN&format=1"
guard let url = URL(string: urlData) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
guard error == nil else { return }
do {
let course = try JSONDecoder().decode(Course.self, from: data)
print(Course())
} catch let error {
print(error)
}
}.resume()
}
}
I have run your code you don't need to use two structures to parse the above JSON data. You can easily get Data in a single Structure. I have modified your code as follows:-
import UIKit
struct Course: Codable {
let success: Bool?
let timestamp: Int?
let base, date: String?
let rates: [String: Double]?
}
class JsonViewContoller: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
courseData()
}
fileprivate func courseData(){
let urlData: String = "http://data.fixer.io/api/latest?access_key=7ac2982c82da926b787fd2f089b110e5&symbols=USD,AUD,CAD,PLN,MXN&format=1"
guard let url = URL(string: urlData) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
guard error == nil else { return }
do {
let course = try JSONDecoder().decode(Course.self, from: data)
print(course)
} catch let error {
print(error)
}
}.resume()
}
}

Swift Error- keyNotFound(CodingKeys(stringValue:, intValue: nil), Swift.DecodingError.Context,

I am trying to read off Connecticut coronavirus data from this API JSON File- "https://data.ct.gov/resource/rf3k-f8fg.json" ( but I'm getting this error from excode that says keyNotFound(CodingKeys(stringValue: "covid19TestsReported", intValue: nil), Swift.DecodingError.Context
However this error, only comes up when I try to access the covid_19_tests_reported property of the file.
Here is my code, can someone please tell me what I'm doing wrong.
import UIKit
class StateViewController: UIViewController {
#IBOutlet weak var testRatioLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getData()
}
fileprivate func getData(){
let url = URL(string: "https://data.ct.gov/resource/rf3k-f8fg.json")!
URLSession.shared.dataTask(with: url){(data, response, error) in
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let users = try decoder.decode([Users].self, from: data!)
print(users)
}
catch {
print(error)
}
}.resume()
}
func didUpdatePrice(tests: String) {
DispatchQueue.main.async {
self.testRatioLbl.text = tests + " tests"
}
}
func didFailWithError(error: Error) {
print(error)
}
}
class Users: Decodable {
let covid19TestsReported: String
enum CodingKeys: String, CodingKey {
case covid19TestsReported = "covid19TestsReported"
}
required init(from decoder:Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
covid19TestsReported = try values.decode(String.self, forKey: .covid19TestsReported)
}
}
update you class.. your key value is "covid_19_tests_reported" not "covid19TestsReported"
because this field is nil in some cases so make it optional
struct Users: Decodable {
let covid19TestsReported: String?
private enum CodingKeys: String, CodingKey {
case covid19TestsReported = "covid_19_tests_reported"
}
}
Also update your getData
fileprivate func getData(){
let url = URL(string: "https://data.ct.gov/resource/rf3k-f8fg.json")!
URLSession.shared.dataTask(with: url){(data, response, error) in
do {
let decoder = JSONDecoder()
let users = try decoder.decode([Users].self, from: data!)
users.forEach { (user) in
if let cases = user.covid19TestsReported {
print(cases)
}
}
}
catch {
print(error)
}
}.resume()
}

Why JsonDecoder Giving Error while trying to parse postman Url but working on other urls?

Both The Url contains Dictionary In Json Format.
import UIKit
import Foundation
struct Course : Decodable{
let foo1: String?
let foo2: String?
let fullName : String?
let numFound : Int?
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let jsonUrl = "https://api.plos.org/search?q=title:DNA" //Working On this Url
let jsonUrl = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"//Not Working On this Url
guard let url = URL(string: jsonUrl) else { return}
URLSession.shared.dataTask(with: url) {(data,response,err) in
guard let data = data
else {return}
do
{
let course = try JSONDecoder().decode([String: Course].self , from: data)
course.forEach { print("\($0.key): \($0.value)")}
}
catch let jerr
{
print(jerr)
}
}.resume()
}
}
This Is the Error If I am Using Postman Url
Paste the Postman URL into a browser and look at the JSON.
It doesn't work because there are other values than Course for other keys so decoding a [String:Course] dictionary fails.
You have to add an umbrella struct for the root object
struct Root : Decodable {
let args : Course
}
struct Course : Decodable {
let foo1: String?
let foo2: String?
let fullName : String?
let numFound : Int?
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let jsonUrl = "https://api.plos.org/search?q=title:DNA" //Working On this Url
let jsonUrl = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"//Not Working On this Url
guard let url = URL(string: jsonUrl) else { return}
URLSession.shared.dataTask(with: url) {(data,response,err) in
guard let data = data else {return}
do {
let result = try JSONDecoder().decode(Root.self , from: data)
print(result.args.foo1, result.args.foo2)
} catch {
print(error)
}
}.resume()
}
}

Decoding Huge JSON Array URL using swift

I'm trying to decode data from this massive JSON Array https://coronavirus-19-api.herokuapp.com/countries I've had luck decoding by country or using the total stat worldwide https://coronavirus-19-api.herokuapp.com/all
by doing the following
//
// GlobalSwiftViewController.swift
// Universal
//
// Created by JOE on 3/20/20.
import UIKit
final class StatSwiftViewController: UIViewController {
// THESE LABELS WILL RETRIEVE THE FOLLOWING DATA FROM THE URL: THE CASE , DEATH AND RECOVERED DATA
#IBOutlet weak var CaseLable: UILabel!
#IBOutlet weak var DeathLable: UILabel!
#IBOutlet weak var RecoveredLabel: UILabel!
struct JSONTest: Decodable {
let cases: Double
let deaths: Float
let recovered: Int?
}
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://coronavirus-19-api.herokuapp.com/all"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
do {
//Decode data
let urlString = try JSONDecoder().decode(JSONTest.self, from: data)
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
//HERE WE ARE SHOWING TO THE USER THE DATA FROM THE URL ABOVE
DispatchQueue.main.async {
self.CaseLable.text = numberFormatter.string(for: urlString.cases)
self.DeathLable.text = numberFormatter.string(for: urlString.deaths)
self.RecoveredLabel.text = numberFormatter.string(for: urlString.recovered)
//self.timeLabel.text = JSONTest.deaths
}
} catch let jsonError {
print(jsonError)
}
}.resume()
}
}
Now I'm trying to decode all of the data in this URL https://coronavirus-19-api.herokuapp.com/countries to show in one view controller, I've had success by using the single URL https://coronavirus-19-api.herokuapp.com/countries/china for the country using the same code above by just adding more vars and labels However, I'm not able to add more counties by adding each URL for each country or using the main URL for all countries https://coronavirus-19-api.herokuapp.com/countries Therefore, How can I struct all Array List using the URL for all countries?
note: Im trying to edit/update my code above to get the results as possible without installing extra pods or files...
Try to adapt your model to be able to decode the countries data.
You can test this in a Playground:
import Foundation
struct JSONTestElement: Codable {
let country: String
let cases, todayCases, deaths, todayDeaths: Int
let recovered, active, critical, casesPerOneMillion: Int
}
typealias JSONTest = [JSONTestElement]
func decode() {
let urlString = "https://coronavirus-19-api.herokuapp.com/countries"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
do {
//Decode data
let countriesData = try JSONDecoder().decode(JSONTest.self, from: data)
let china = countriesData.filter({ $0.country.contains("China")})
print("China data: \(china)")
} catch let jsonError {
print(jsonError)
}
}.resume()
}
decode()

Resources