I need to create a table view with a nested Json - json

I have to read a json from the url: https://randomuser.me/api/?results=100
I created the People.swift file that contains the structure created through the site: https://app.quicktype.io/?l=swift
I tried to use this code but I can not then insert the json into the structure and then recall it via cell.people.name for example.
ViewController.swift:
var dataRoutine = [People]() // this is the structure that I created with the site indicated above.
this one is my function to download the Json and parse.
func downloadJsonData(completed : #escaping ()->()){
guard let url = URL(string: "https://randomuser.me/api/?results=100")else {return}
let request = URLRequest.init(url: url)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let httpResponse = response as? HTTPURLResponse {
let statuscode = httpResponse.statusCode
if statuscode == 404{
print( "Sorry! No Routine Found")
}else{
if error == nil{
do{
self.dataRoutine = try JSONDecoder().decode(People.self, from: data!)
DispatchQueue.main.async {
completed()
print(self.dataRoutine.count) // I don't know why my result is ever 1.
}
}catch{
print(error)
}
}
}
}
}.resume()
}
my structure is :
import Foundation
struct People: Codable {
let results: [Result]?
let info: Info?
}
struct Info: Codable {
let seed: String?
let results, page: Int?
let version: String?
}
struct Result: Codable {
let gender: Gender?
let name: Name?
let location: Location?
let email: String?
let login: Login?
let dob, registered: Dob?
let phone, cell: String?
let id: ID?
let picture: Picture?
let nat: String?
}
struct Dob: Codable {
let date: Date?
let age: Int?
}
enum Gender: String, Codable {
case female = "female"
case male = "male"
}
struct ID: Codable {
let name: String?
let value: String?
}
struct Location: Codable {
let street, city, state: String?
let postcode: Postcode?
let coordinates: Coordinates?
let timezone: Timezone?
}
struct Coordinates: Codable {
let latitude, longitude: String?
}
enum Postcode: Codable {
case integer(Int)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Int.self) {
self = .integer(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(Postcode.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Postcode"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}
struct Timezone: Codable {
let offset, description: String?
}
struct Login: Codable {
let uuid, username, password, salt: String?
let md5, sha1, sha256: String?
}
struct Name: Codable {
let title: Title?
let first, last: String?
}
enum Title: String, Codable {
case madame = "madame"
case mademoiselle = "mademoiselle"
case miss = "miss"
case monsieur = "monsieur"
case mr = "mr"
case mrs = "mrs"
case ms = "ms"
}
struct Picture: Codable {
let large, medium, thumbnail: String?
}

The main issue is a type mismatch.
The root object of the JSON is not the People array, it's the umbrella struct, I named it Response
Please change the structs to
struct Response: Decodable {
let results: [Person]
let info: Info
}
struct Info: Decodable {
let seed: String
let results, page: Int
let version: String
}
struct Person: Decodable {
let gender: Gender
let name: Name
let location: Location
let email: String
let login: Login
let dob, registered: Dob
let phone, cell: String
let id: ID
let picture: Picture
let nat: String
}
struct Dob: Decodable {
let date: Date
let age: Int
}
enum Gender: String, Decodable {
case female, male
}
struct ID: Codable {
let name: String
let value: String?
}
struct Location: Decodable {
let street, city, state: String
let postcode: Postcode
let coordinates: Coordinates
let timezone: Timezone
}
struct Coordinates: Codable {
let latitude, longitude: String
}
enum Postcode: Codable {
case integer(Int)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Int.self) {
self = .integer(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(Postcode.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Postcode"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}
struct Timezone: Codable {
let offset, description: String
}
struct Login: Codable {
let uuid, username, password, salt: String
let md5, sha1, sha256: String
}
struct Name: Codable {
let title: Title
let first, last: String
}
enum Title: String, Codable {
case madame, mademoiselle, miss, monsieur, mr, mrs, ms
}
struct Picture: Codable {
let large, medium, thumbnail: String
}
Almost all properties can be declared non-optional, the date key in Dob is an ISO8601 date string you have to add the appropriate date decoding strategy. The People array is the property results of the root object.
var dataRoutine = [Person]()
func downloadJsonData(completed : #escaping ()->()){
guard let url = URL(string: "https://randomuser.me/api/?results=100")else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let httpResponse = response as? HTTPURLResponse {
let statuscode = httpResponse.statusCode
if statuscode == 404{
print( "Sorry! No Routine Found")
}else{
if error == nil{
do{
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let jsonResponse = try decoder.decode(Response.self, from: data!)
self.dataRoutine = jsonResponse.results
DispatchQueue.main.async {
completed()
print(self.dataRoutine.count) // I don't know why my result is ever 1.
}
}catch{
print(error)
}
}
}
}
}.resume()
}

Related

Generic decoding json into object with optionals

I wrote an extension to Decodable with the hopes of having a generic constructor for objects from json strings, it looks like this:
extension Decodable {
init?(with dictionary: [String: Any]) {
guard let data = try? JSONSerialization.data(
withJSONObject: dictionary,
options: .prettyPrinted
) else {
return nil
}
guard let result = try? JSONDecoder().decode(
Self.self,
from: data
) else {
return nil
}
self = result
}
}
An example use case looks like this:
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { return }
guard var goer = Goer(with: json) else { return }
And the object I'm trying to decode into looks like this:
struct Goer: Codable {
let goerId: String
let created: Double
let username: String
let firstName: String
let lastName: String
let city: String
let bio: String
let isPrivate: Bool
let numFollowers: Int
let numFollowing: Int
let followers: [GoerFollow]
let following: [GoerFollow]
}
My issue is that I want to introduce some optionals to these objects that the json strings I'm trying to decode may or may not have keys for. This generic constructor fails in the case where there is no key:value in the json for an optional variable in the object.
I've seen that I can write a custom constructor with a decoder for each object and use the decodeIfPresent function but I wonder if there is a way to do it generically.
if let jsonData = data {
do {
var model = try decoder.decode(Goer.self, from: jsonData)
print("model:\(model)")
} catch {
print("error:\(error)")
}
}
struct Goer: Codable {
let goerId: String?
let created: Double?
let username: String?
let firstName: String?
let lastName: String?
let city: String?
let bio: String?
let isPrivate: Bool?
let numFollowers: Int?
let numFollowing: Int?
let followers: [GoerFollow]?
let following: [GoerFollow]?
}

How to use decodable protocol with custom type values in Swift?

I have 2 types of response depending on my reuest: First one:
{
"status": "success"
"data": {
"user_id": 2,
"user_name": "John"
}
}
And second one is:
{
"status": "error",
"data": [],
}
I am using struct like that:
struct ValyutaListData:Decodable {
let status: String?
let data: [String]?
}
But if response is first type response, then an error occured. Because In first Type response data is not array. It is Json object. Then i use structure like that:
struct ValyutaListData:Decodable {
let status: String?
let data: Persondata?
}
struct Persondata: Decodable{
let user_id: Int?
let user_name: String?
}
If response is second type response, the error will be occured. What kind of of structure should use for dynamic type JSONs? Thanks.
One reasonable solution is an enum with associated type(s)
struct User : Decodable {
let userId: Int
let userName: String
}
enum Result : Decodable {
case success(User), failure
enum CodingKeys: String, CodingKey { case status, data }
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let status = try container.decode(String.self, forKey: .status)
if status == "success" {
let userData = try container.decode(User.self, forKey: .data)
self = .success(userData)
} else {
self = .failure
}
}
}
And use it
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Result.self, from: data)
switch result {
case .success(let user): print(user)
case .failure: print("An error occurred")
}
} catch { print(error) }

Decoding unnamed JSON array swift 4 with structure

I have been trying to decode an array for an app where I have to decode JSON.
I just can't decode it with my actual structure and others which I tried before.
Here are my latest structures:
struct peticion: Decodable{
let datos_conexion: datos_conexion
let estado_lanzamiento: estado_lanzamiento
let usuario: usuario
}
struct datos_conexion: Decodable {
let conexion: datosConexion
}
struct datosConexion: Decodable{
let datos_conexion: String
}
struct estado_lanzamiento: Decodable{
let tiempo_restante: String
let etapa_actual: String
}
struct usuario: Decodable {
let Id: Int
let Nombre: String
let Password: String
let Imagen: String
let Puesto: String
let Departamento: String
}
JSON full example from Request
[
{
"datos_conexion": {
"conexion": "2019-05-27 17:05:45"
}
},
{
"estado_lanzamiento": {
"tiempo_restante": 240,
"etapa_actual": "Configuracion"
}
},
{
"usuario": [
{
"Id": "4",
"Nombre": "This is the Name",
"Email": "email#gmail.com",
"Password": "1234",
"Imagen": "default.jpg",
"Puesto": "",
"Departamento": "Etapa Final"
}
]
}
]
decoding code
URLSession.shared.dataTask(with: url2) { (data, resp, err) in
guard let data = data else{return}
let dataAsString = String(data: data, encoding: .utf8)
// print(dataAsString)
do {
let JSONDATA = try JSONDecoder().decode([peticion].self, from: data)
// print(data)
} catch let jsonErr {
print("cant decode", jsonErr)
}
Errors trying to do this:
typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode dictionary but found an Array instead.", underlyingError: nil))
Based on your JSON, you are receiving an [[String: Any]] which is Array containing Dictionaries of Any values and String keys. This is tricky, since the dictionaries are containing values of Any which is not Decodable. Also, the container is Array, usually we would have a Dictionary as the container.
Normally, we would simplify this response.. But we cannot on this scenario.
One way to decode this is:
struct Peticion: Decodable{
let datosConexion: DatosConexion
let estadoLanzamiento: EstadoLanzamiento
let usuario: [Usuario]
init(from decoder : Decoder) throws {
//unkeyed because we are getting an array as container
var unkeyedContainer = try decoder.unkeyedContainer()
let datosConexionWrapper = try unkeyedContainer.decode(DatosConexionWrapper.self)
let estadoLanzamientoWrapper = try unkeyedContainer.decode(EstadoLanzamientoWrapper.self)
let usuarioWrapper = try unkeyedContainer.decode(UsuarioWrapper.self)
datosConexion = datosConexionWrapper.datosConexion
estadoLanzamiento = estadoLanzamientoWrapper.estadoLanzamiento
usuario = usuarioWrapper.usuario
}
}
//use wrappers to handle the outer dictionary
struct DatosConexionWrapper: Decodable{
let datosConexion: DatosConexion
enum CodingKeys: String, CodingKey{
case datosConexion = "datos_conexion"
}
}
struct DatosConexion: Decodable{
let conexion: String
}
struct EstadoLanzamientoWrapper: Decodable{
let estadoLanzamiento: EstadoLanzamiento
enum CodingKeys: String, CodingKey{
case estadoLanzamiento = "estado_lanzamiento"
}
}
struct EstadoLanzamiento: Decodable{
let tiempoRestante: Int
let etapaActual: String
enum CodingKeys: String, CodingKey {
case tiempoRestante = "tiempo_restante"
case etapaActual = "etapa_actual"
}
}
struct UsuarioWrapper: Decodable{
let usuario: [Usuario]
}
struct Usuario: Decodable{
let id: String
let nombre: String
let email: String
let password: String
let imagen: String
let puesto: String
let departamento: String
enum CodingKeys: String, CodingKey {
case id = "Id"
case nombre = "Nombre"
case email = "Email"
case password = "Password"
case imagen = "Imagen"
case puesto = "Puesto"
case departamento = "Departamento"
}
}
Decoding code //i only changed the decoding type to my Peticion struct
URLSession.shared.dataTask(with: url2) { (data, resp, err) in
guard let data = data else{return}
let dataAsString = String(data: data, encoding: .utf8)
// print(dataAsString)
do {
let JSONDATA = try JSONDecoder().decode(Peticion.self, from: data)
// print(data)
} catch let jsonErr {
print("cant decode", jsonErr)
}
This was a fun exercise. This is also untested code, but I'm pretty sure it would work.
import Foundation
// MARK: - WelcomeElement
struct WelcomeElement: Codable {
let datosConexion: DatosConexion?
let estadoLanzamiento: EstadoLanzamiento?
let usuario: [Usuario]?
enum CodingKeys: String, CodingKey {
case datosConexion = "datos_conexion"
case estadoLanzamiento = "estado_lanzamiento"
case usuario
}
}
// MARK: - DatosConexion
struct DatosConexion: Codable {
let conexion: String
}
// MARK: - EstadoLanzamiento
struct EstadoLanzamiento: Codable {
let tiempoRestante: Int
let etapaActual: String
enum CodingKeys: String, CodingKey {
case tiempoRestante = "tiempo_restante"
case etapaActual = "etapa_actual"
}
}
// MARK: - Usuario
struct Usuario: Codable {
let id, nombre, email, password: String
let imagen, puesto, departamento: String
enum CodingKeys: String, CodingKey {
case id = "Id"
case nombre = "Nombre"
case email = "Email"
case password = "Password"
case imagen = "Imagen"
case puesto = "Puesto"
case departamento = "Departamento"
}
}
typealias Welcome = [WelcomeElement]

Deserialization JSON swift 4.2

I try to deserialize my JSON by using Decodable protocol, also i use enum with CodingKey, but it doesn't work. I need only nested array (start with "indicator"), and only few fields (all of them in struct). I tried a lot of different options, but unfortunately..
P.S. Also i tried to do it without CodingKey. Anyway response was: "Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "country", intValue: nil)" Ofc i read this, maybe array is a reason(i mean this strange intValue)?
JSON
[
{
"page":1,
"pages":2,
"per_page":50,
"total":59,
"sourceid":"2",
"lastupdated":"2019-03-21"
},
[
{
"indicator":{
"id":"IP.PAT.RESD",
"value":"Patent applications, residents"
},
"country":{
"id":"SS",
"value":"South Sudan"
},
"countryiso3code":"SSD",
"date":"2018",
"value":null,
"unit":"",
"obs_status":"",
"decimal":0
},
{
"indicator":{
"id":"IP.PAT.RESD",
"value":"Patent applications, residents"
},
"country":{
"id":"SS",
"value":"South Sudan"
},
"countryiso3code":"SSD",
"date":"2017",
"value":null,
"unit":"",
"obs_status":"",
"decimal":0
},
...
]
]
My code
struct CountryObject: Decodable{
var country: CountryInfo
var date: Int
var value: Int?
private enum RawValues: String, Decodable{
case date = "date"
case vallue = "value"
}
}
struct CountryInfo: Decodable{//Country names
var id: String?
var value: String?
private enum RawValues: String, Decodable{
case id = "id"
case value = "value"
}
}//
let urlString = "https://api.worldbank.org/v2/country/SS/indicator/IP.PAT.RESD?format=json"
guard let url = URL(string: urlString) else {return}
URLSession.shared.dataTask(with: url) {(data,response,error) in
guard let data = data else {return}
guard error == nil else {return}
do{
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let countryObject = try! decoder.decode([CountryObject].self, from: data)
print(countryObject)
}catch let error{
print(error)
}
}.resume()
Create a root struct and decode the array with unkeyedContainer
struct Root : Decodable {
let info : Info
let countryObjects : [CountryObject]
init(from decoder: Decoder) throws {
var arrayContrainer = try decoder.unkeyedContainer()
info = try arrayContrainer.decode(Info.self)
countryObject = try arrayContrainer.decode([CountryObject].self)
}
}
struct Info : Decodable {
let page, pages, perPage: Int
let lastupdated: String
}
struct CountryObject : Decodable {
let country: CountryInfo
let date: String
let value: Int?
}
struct CountryInfo : Decodable { //Country names
let id: String
let value: String
}
...
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let root = try decoder.decode(Root.self, from: data)
let countryObjects = root.countryObjects
print(countryObjects)
} catch { print(error) }
(De)serializing the JSON twice is unnecessarily expensive.

Unable to fetch data from json through codable swift

This is json code:
{
"status":"success",
"data":
[
{"id":"3",
"city_name":"Delhi",
"city_image":"delhi.png"},
{"id":"4",
"city_name":"Mumbai",
"city_image":"tickmark.png"}
]
}
My Swift Code :
struct city: Decodable{
let status : String
let id: String
let data : String
let city_name: String
let city_image: String
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let jsonUrl = "http://parking.onlinekiduniya.org/api/cityList.php"
let url = URL(string: jsonUrl)
URLSession.shared.dataTask(with: url!) {(data, response, error) in
do {
let cities = try JSONDecoder().decode([city].self, from: data!)
for city in cities {
print(city.id)
}
}
catch {
print("we got error")
}
}.resume()
}
}
Replace
let cities = try JSONDecoder().decode([city].self, from: data!)
with
let root = try JSONDecoder().decode(Root.self, from: data!)
let cities = root.data
cities.forEach {
print($0.id)
}
struct Root: Codable {
let status: String
let data: [City]
}
struct City: Codable {
let id, cityName, cityImage: String // you can use snake case also
enum CodingKeys: String, CodingKey {
case id
case cityName = "city_name"
case cityImage = "city_image"
}
}
Your root is a dictionary not an array