How to add arrays of objects when deserializing with SwiftyJSON - arrays

I am working on deserializing JSON with SwiftyJSON and Swift 5.1.
I have a set of data like below
"name": "American Standard",
"number": 1,
"subcategories": [
{
"name": "American Light Lager",
"letter": "A",
"guidelines": {
"overallImpression": "Some String",
"aroma": "Some String",
"appearance": "Some String",
"flavor": "Some String",
"mouthfeel": "Some String",
"comments": "Some String",
"history": "Some String",
"ingredients": "Some String",
"comparison": "Some String",
"vitalStatistics": {
"og": "1.028 - 1.040",
"fg": "0.998 - 1.008",
"abv": "2.8 - 4.2%",
"ibu": "8 - 12",
"srm": "2 - 3"
}
},
"commercialExamples": [
{
"name": "name1"
},
{
"name": "name2"
},
{
"name": "name3"
},
],
"tags": [
{
"tag": "tag1"
},
{
"tag": "tag2"
},
]
},
I am using struct to hold all the data, shown below.
struct Beers
{
var name: String
var number: Int
var subcategories: Subcategory
}
struct Subcategory
{
var name: String
var letter: String
var guidelines: Guidelines
var commercialExamples: [CommercialExample]
var tags: [Tag]
}
struct Guidelines
{
var overallImpression: String
var aroma: String
var appearance: String
var flavor: String
var mouthfeel: String
var comments: String
var history: String
var ingredients: String
var comparison: String
var vitalStatistics: VitalStatistics
}
struct VitalStatistics
{
var og: String
var fg: String
var abv: String
var ibu: String
var srm: String
}
struct CommercialExample : Hashable
{
var name: String
func hash(into hasher: inout Hasher)
{
hasher.combine(name)
}
}
struct Tag : Hashable
{
var tag: String
func hash(into hasher: inout Hasher)
{
hasher.combine(tag)
}
}
And for my deserialization code, I have this.
for (index, dict) in jsonObject
{
let thisBeer = Beers(name: dict["name"].stringValue, number: dict["number"].intValue, subcategories: Subcategory(name: dict["name"].stringValue, letter: dict["letter"].stringValue, guidelines: Guidelines(overallImpression: dict["overallImpression"].stringValue, aroma: dict["aroma"].stringValue, appearance: dict["appearance"].stringValue, flavor: dict["flavor"].stringValue, mouthfeel: dict["mouthfeel"].stringValue, comments: dict["comments"].stringValue, history: dict["history"].stringValue, ingredients: dict["ingredients"].stringValue, comparison: dict["comparison"].stringValue, vitalStatistics: VitalStatistics(og: dict["og"].stringValue, fg: dict["fg"].stringValue, abv: dict["abv"].stringValue, ibu: dict["ibu"].stringValue, srm: dict["srm"].stringValue)), commercialExamples: CommercialExample(name: dict["name"].stringValue), tags: Tag(tags: dict["tags"].stringValue)))
beers.append(thisBeer)
}
This is where I am stuck. I am much more familiar with C# and .net. I just don't know how to go about looping through the commercialExamples and tags and creating the objects from the data and populating the arrays with them. What is the proper way of doing this with Swift and SwiftyJSON?

You must first use JSONSerializable protocols to decode your JSON data into the object. For example in your case:
struct Beers: JSONSerializable {
var name: String?
var number: Int?
var subcategories: Subcategory?
enum CodingKeys: String, CodingKey {
case name
case number
case subcategories
}
}
extension Beers: Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if let value = try values.decodeIfPresent(String?.self, forKey: .name) {
if let value = value {
name = value
}
}
if let value = try values.decodeIfPresent(Int?.self, forKey: .number) {
if let value = value {
number = value
}
}
if let value = try values.decodeIfPresent(Subcategory?.self, forKey: .subcategories) {
if let value = value {
subcategories = value
}
}
}
}
Then you can make an object from JSON data like this:
do {
let model = try JSONDecoder().decode(Beers.self, from: jsonData)
} catch let error {
print(error)
}

Related

Can not display properly data in array in nested complex Json with Swift

I'm tring to display in a Text view a nested part of a json content but I'm not able to do it correctly. For example I can't display properly the response of [Connector], any thoughts ?
Here is my struct :
import Foundation
import SwiftUI
// MARK: - StationDataApi
struct StationDataApi: Codable {
var summary: Summary
var results: [Result]
enum CodingKeys: String, CodingKey {
case summary = "summary"
case results = "results"
}
}
// MARK: - Result
struct Result: Codable {
var type: ResultType
var id: String
var score: Double
var dist: Double
var info: String
var poi: Poi
var address: Address
var position: GeoBias
var viewport: Viewport
var entryPoints: [EntryPoint]
var chargingPark: ChargingPark
var dataSources: DataSources?
enum CodingKeys: String, CodingKey {
case type = "type"
case id = "id"
case score = "score"
case dist = "dist"
case info = "info"
case poi = "poi"
case address = "address"
case position = "position"
case viewport = "viewport"
case entryPoints = "entryPoints"
case chargingPark = "chargingPark"
case dataSources = "dataSources"
}
}
// MARK: - Address
struct Address: Codable {
var streetName: String
var municipality: Country
var countrySubdivision: Country
var postalCode: String
var countryCode: CountryCode
var country: Country
var countryCodeIso3: CountryCodeIso3
var freeformAddress: String
var localName: Country
var streetNumber: String?
var municipalitySubdivision: String?
enum CodingKeys: String, CodingKey {
case streetName = "streetName"
case municipality = "municipality"
case countrySubdivision = "countrySubdivision"
case postalCode = "postalCode"
case countryCode = "countryCode"
case country = "country"
case countryCodeIso3 = "countryCodeISO3"
case freeformAddress = "freeformAddress"
case localName = "localName"
case streetNumber = "streetNumber"
case municipalitySubdivision = "municipalitySubdivision"
}
}
enum Country: String, Codable {
case luxembourg = "Luxembourg"
}
enum CountryCode: String, Codable {
case lu = "LU"
}
enum CountryCodeIso3: String, Codable {
case lux = "LUX"
}
// MARK: - ChargingPark
struct ChargingPark: Codable {
var connectors: [Connector]
enum CodingKeys: String, CodingKey {
case connectors = "connectors"
}
}
// MARK: - Connector
struct Connector: Codable {
var connectorType: String
var ratedPowerKw: Int
var voltageV: Int
var currentA: Int
var currentType: CurrentType
enum CodingKeys: String, CodingKey {
case connectorType = "connectorType"
case ratedPowerKw = "ratedPowerKW"
case voltageV = "voltageV"
case currentA = "currentA"
case currentType = "currentType"
}
}
enum CurrentType: String, Codable {
case ac3 = "AC3"
case dc = "DC"
}
// MARK: - DataSources
struct DataSources: Codable {
var chargingAvailability: ChargingAvailability
enum CodingKeys: String, CodingKey {
case chargingAvailability = "chargingAvailability"
}
}
// MARK: - ChargingAvailability
struct ChargingAvailability: Codable {
var id: String
enum CodingKeys: String, CodingKey {
case id = "id"
}
}
// MARK: - EntryPoint
struct EntryPoint: Codable {
var type: EntryPointType
var position: GeoBias
enum CodingKeys: String, CodingKey {
case type = "type"
case position = "position"
}
}
// MARK: - GeoBias
struct GeoBias: Codable {
var lat: Double
var lon: Double
enum CodingKeys: String, CodingKey {
case lat = "lat"
case lon = "lon"
}
}
enum EntryPointType: String, Codable {
case main = "main"
}
// MARK: - Poi
struct Poi: Codable {
var name: String
var phone: String?
var categorySet: [CategorySet]
var categories: [Query]
var classifications: [Classification]
enum CodingKeys: String, CodingKey {
case name = "name"
case phone = "phone"
case categorySet = "categorySet"
case categories = "categories"
case classifications = "classifications"
}
}
enum Query: String, Codable {
case electricVehicleStation = "electric vehicle station"
}
// MARK: - CategorySet
struct CategorySet: Codable {
var id: Int
enum CodingKeys: String, CodingKey {
case id = "id"
}
}
// MARK: - Classification
struct Classification: Codable {
var code: Code
var names: [Name]
enum CodingKeys: String, CodingKey {
case code = "code"
case names = "names"
}
}
enum Code: String, Codable {
case electricVehicleStation = "ELECTRIC_VEHICLE_STATION"
}
// MARK: - Name
struct Name: Codable {
var nameLocale: NameLocale
var name: Query
enum CodingKeys: String, CodingKey {
case nameLocale = "nameLocale"
case name = "name"
}
}
enum NameLocale: String, Codable {
case enUs = "en-US"
}
enum ResultType: String, Codable {
case poi = "POI"
}
// MARK: - Viewport
struct Viewport: Codable {
var topLeftPoint: GeoBias
var btmRightPoint: GeoBias
enum CodingKeys: String, CodingKey {
case topLeftPoint = "topLeftPoint"
case btmRightPoint = "btmRightPoint"
}
}
// MARK: - Summary
struct Summary: Codable {
var query: Query
var queryType: String
var queryTime: Int
var numResults: Int
var offset: Int
var totalResults: Int
var fuzzyLevel: Int
var geoBias: GeoBias
enum CodingKeys: String, CodingKey {
case query = "query"
case queryType = "queryType"
case queryTime = "queryTime"
case numResults = "numResults"
case offset = "offset"
case totalResults = "totalResults"
case fuzzyLevel = "fuzzyLevel"
case geoBias = "geoBias"
}
Here is an extract of the Json response :
{
"summary": {
"query": "electric vehicle station",
"queryType": "NON_NEAR",
"queryTime": 130,
"numResults": 10,
"offset": 0,
"totalResults": 232894,
"fuzzyLevel": 1,
"geoBias": {
"lat": 49.588056,
"lon": 6.104167
}
},
"results": [
{
"type": "POI",
"id": "442009000702915",
"score": 8.495806694,
"dist": 357.116545666857,
"info": "search:ev:442009000702915",
"poi": {
"name": "Chargy Luxembourg Place de Roedgen",
"phone": "+352 800 62 020",
"categorySet": [
{
"id": 7309
}
],
"categories": [
"electric vehicle station"
],
"classifications": [
{
"code": "ELECTRIC_VEHICLE_STATION",
"names": [
{
"nameLocale": "en-US",
"name": "electric vehicle station"
}
]
}
]
},
"address": {
"streetName": "Place de Roedgen",
"municipality": "Luxembourg",
"countrySubdivision": "Luxembourg",
"postalCode": "L-2432",
"countryCode": "LU",
"country": "Luxembourg",
"countryCodeISO3": "LUX",
"freeformAddress": "Place de Roedgen, Luxembourg, L-2432",
"localName": "Luxembourg"
},
"position": {
"lat": 49.59111,
"lon": 6.1057
},
"viewport": {
"topLeftPoint": {
"lat": 49.59201,
"lon": 6.10431
},
"btmRightPoint": {
"lat": 49.59021,
"lon": 6.10709
}
},
"entryPoints": [
{
"type": "main",
"position": {
"lat": 49.59104,
"lon": 6.10555
}
}
],
"chargingPark": {
"connectors": [
{
"connectorType": "IEC62196Type2Outlet",
"ratedPowerKW": 22,
"voltageV": 400,
"currentA": 32,
"currentType": "AC3"
}
]
},
"dataSources": {
"chargingAvailability": {
"id": "442009000702915"
}
}
}
Here is my Network code :
func loadData() async{
guard let url = Bundle.main.url(forResource: "response", withExtension: "json")
else {
print("Json file not found")
return
}
do {
let (data, _) = try await URLSession.shared.data(from: url)
if let decodedResponse = try? JSONDecoder().decode(StationDataApi.self, from: data) {
results=decodedResponse.results
}
} catch {
print("Invalid data")
}
}
Here is the my View :
struct StationView: View {
#State private var results : [Result] = []
var body: some View {
NavigationView {
List (results, id: \.id){item in
NavigationLink(destination: DetailStationView(stationItem: item)){
VStack(alignment: .leading) {
Text(item.chargingPark.connectors.description)
}
}
}
}
.navigationTitle("Fetch")
.task {
await loadData()
}
}
That is the result that I get. I wanted to display it properly not like a raw json content
connectors is an array of Connector, and so you need to decide which
array element you want to display. For example all elements, such as:
VStack(alignment: .leading) {
ForEach(item.chargingPark.connectors, id: \.self) { conector in
Text("connectorType: \(conector.connectorType)")
Text("PowerKw: \(conector.ratedPowerKw)")
Text("voltageV: \(conector.voltageV)")
Text("currentA: \(conector.currentA)")
Text("currentType: \(conector.currentType.rawValue)")
}
}
or the first one as in this example:
VStack(alignment: .leading) {
Text("\(item.chargingPark.connectors.first?.ratedPowerKw ?? 0)")
Text("\(item.chargingPark.connectors.first?.voltageV ?? 0)")
Text("\(item.chargingPark.connectors.first?.currentA ?? 0)")
Text("\(item.chargingPark.connectors.first?.currentType.rawValue ?? "")")
}
Adjust the code and display to your specific needs. Note you may need to use struct Connector: Codable, Hashable {...} or make Connector Identifiable.

How to parse array data from Yelp API?

Allo, Hi, im new with SwiftUI and im facing some issue with the Yelp API and can't find answer anywhere... I'm creating a restaurant app and I want to add a on the business detail page a list of Yelp transactions that the business is registered for ("pickup", "delivery", and "restaurant_reservation").
I've try a lot of way to retrieved it but im about to give up... I don't know if it's me who's dumb or anything but my brain can't figured it out anymore. I've tried to get the data with "ForEach" and all any other way we usually get array data...
Second question (similar as the previous one) how can I retrieve the category alias/title from the API? I want to be able to filter the business based on their categories and also show on the business detail page the category associated to it.
Thank you :)
Yelp Response Body Example :
{
"total": 144,
"businesses": [
{
"id": "gR9DTbKCvezQlqvD7_FzPw",
"alias": "north-india-restaurant-san-francisco",
"price": "$$",
"url": "https://www.yelp.com/biz/north-india-restaurant-san-francisco",
"rating": 4,
"location": {
"zip_code": "94105",
"state": "CA",
"country": "US",
"city": "San Francisco",
"address2": "",
"address3": "",
"address1": "123 Second St"
},
"categories": [
{
"alias": "indpak",
"title": "Indian"
}
],
"phone": "+14153481234",
"coordinates": {
"longitude": -122.399305736113,
"latitude": 37.787789124691
},
"image_url": "http://s3-media4.fl.yelpcdn.com/bphoto/howYvOKNPXU9A5KUahEXLA/o.jpg",
"is_closed": false,
"name": "North India Restaurant",
"review_count": 615,
"transactions": ["pickup", "restaurant_reservation"]
},
// ...
]
}
Here is my Business model :
class Business: Decodable, Identifiable, ObservableObject {
#Published var imageData: Data?
var id: String?
var alias: String?
var name: String?
var imageUrl: String?
var isClosed: Bool?
var url: String?
var reviewCount: Int?
var categories: [Category]?
var rating: Double?
var coordinates: Coordinate?
var transactions: [String]?
var price: String?
var location: Location?
var phone: String?
var displayPhone: String?
var distance: Double?
enum CodingKeys: String, CodingKey {
case imageUrl = "image_url"
case isClosed = "is_closed"
case reviewCount = "review_count"
case displayPhone = "display_phone"
case id
case alias
case name
case url
case categories
case rating
case coordinates
case transactions
case price
case location
case phone
case distance
}
func getImageData() {
// Check that image url isn't nil
guard imageUrl != nil else {
return
}
// Download the data for the image
if let url = URL(string: imageUrl!) {
// Get a session
let session = URLSession.shared
let dataTask = session.dataTask(with: url) { (data, response, error) in
if error == nil {
DispatchQueue.main.async {
// Set the image data
self.imageData = data!
}
}
}
dataTask.resume()
}
}
static func getTestData() -> Business {
let b = Business()
return b
}
}
struct Category: Decodable {
var alias: String?
var title: String?
}
Here an example of my code :
struct BusinessDetail: View {
var business: Business
#State private var showDirections = false
var body: some View {
VStack (alignment: .leading) {
VStack (alignment:.leading, spacing:0) {
GeometryReader() { geometry in
// Business image
let uiImage = UIImage(data: business.imageData ?? Data())
Image(uiImage: uiImage ?? UIImage())
.resizable()
.scaledToFill()
.frame(width: geometry.size.width, height: geometry.size.height)
.clipped()
}
.ignoresSafeArea(.all, edges: .top)
// Open / closed indicator
ZStack (alignment: .leading) {
Rectangle()
.frame(height: 35)
.foregroundColor(business.isClosed! ? Color("icon-primary") : Color("background"))
Text(business.isClosed! ? "Closed" : "Open")
.foregroundColor(.white)
.font(.textHeader)
.padding(.leading)
}
}
Group {
HStack {
BusinessTitle(business: business)
.padding()
Spacer()
}
// Phone
HStack {
Text("Phone:")
.bold()
Text(business.displayPhone ?? "")
Spacer()
Link("Call", destination: URL(string: "tel:\(business.phone ?? "")")!)
}
.padding()
// Transactions
if business.transactions != nil {
ForEach(business.transactions!, id: \.self) { transaction in
Text(transaction)
.font(.bodyParagraph)
}
}

Swift JSON objects decoding error. How to write it correctly?

I have a data decoding error. How to write it correctly?
typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
My JSON object:
struct LeagueDocument: Codable {
var id: LeagueId
var name: LeagueName
var area: Area
var startDate: String
var endDate: String
init(id: LeagueId, name: LeagueName, area: Area, startDate: String, endDate: String) {
self.id = id
self.name = name
self.area = area
self.startDate = startDate
self.endDate = endDate
}
}
enum Area: String, Codable {
case POLAND, ENGLAND, FRANCE, SPAIN, GERMANY, ITALY, EUROPE, UEFA_CHAMPIONS_LEAGUE, EUROPE_LEAGUE
}
JSON which I got from server:
[
{
"id": "ba4ddfd6-cb91-4ea7-853d-79be89917445",
"name": "Liga 1 ",
"area": "POLAND",
"startDate": "2021-02-28",
"endDate": "2022-02-28"
},
{
"id": "4061b62b-dd57-4916-adf4-e0874ec767b1",
"name": "Liga 2",
"area": "POLAND",
"startDate": "2021-02-01",
"endDate": "2021-05-11"
}
]
My code:
func getAllActiveLeagues(completion: #escaping ([LeagueDocument]) -> Void) {
(...)
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard error == nil else { print(error!.localizedDescription); return }
if data != nil {
do {
let decodedData = try decoder.decode([LeagueDocument].self, from: data!)
completion(decodedData)
} catch {
print(error)
}
} else {
print("Data is nil")
}
}.resume()

Display items from nested array in SwiftUI

I'm trying to decode Yelp's JSON. Within the file there's a nested array of categories. Some businesses have 1, others may have 3.
Here's an example
{
"businesses": [
{
"id": "4jW-ZDeCPIl9aXvTWcATlA",
"alias": "the-delaney-hotel-orlando",
"name": "The Delaney Hotel",
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/fikUF4yC5J63f3EOCZ8uOw/o.jpg",
"is_closed": false,
"url": "https://www.yelp.com/biz/the-delaney-hotel-orlando?adjust_creative=s-hyKAjsx6P4UW-uqMn7aQ&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=s-hyKAjsx6P4UW-uqMn7aQ",
"review_count": 13,
"categories": [
{
"alias": "hotels",
"title": "Hotels"
},
{
"alias": "venues",
"title": "Venues & Event Spaces"
}
],
"rating": 5.0,
...etc
}
]
My data model is set up this way
struct BusinessesResponse: Codable {
enum CodingKeys: String, CodingKey {
case restaurants = "businesses"
}
let restaurants: [RestaurantResponse]
}
struct RestaurantResponse: Codable, Identifiable, Equatable {
let id: String
var name: String
var image_url: String
var is_closed: Bool
var review_count: Int
var rating: Double
var distance: Double
var price: String?
var display_phone: String?
var categories: [HotelCategory]
var coordinates: HotelCoordinates
var location: HotelLocation
var transactions: [String]
}
struct HotelCategory: Hashable, Codable {
var title: String
}
struct HotelCoordinates: Hashable, Codable {
var latitude: Double
var longitude: Double
}
struct HotelLocation: Hashable, Codable {
var address1: String
var city: String
var state: String
var zip_code: String
}
I'm able to display everything other than the first pair in the categories array. My guess is that I would need to set a ForEach statement to display all available information from that array, but I'm not sure how to set that up correctly.
Here's what I have currently in abbreviated form
var category: [HotelCategory]
var body: some View {
HStack {
Text("\(category[0].title)")
}
}
Obviously that would only return the first child of that nested array. How would I dynamically account for that nested array having multiple children and then displaying them?
You can use ForEach this way:
ForEach(category, id: \.self) { category in
HStack {
Text("\(category.title)")
}
}
Note: if you're not sure that categories are unique, it might be better to conform HotelCategory to Identifiable and use id: \.id.
I'd also recommend following the convention and naming arrays with plural names, i.e.:
var categories: [HotelCategory] // instead of `category`

Decoding JSON array on Swift

I have a simple array that I want to decode. I can deal with arrays in a JSON format with creating a new struct in the model class, and attributing that struct as an array in the main struct. But in this case, the json data is already on an array with 1 element. Therefore I should get the first element in the json response. I think I need to provide a decoder before I can access anything, but I don't know how that decoder model should be. The error I'm getting is:
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
JSON data I want to decode: (notice the data is on a array)
[
{
"name": "United States",
"topLevelDomain": [
".us"
],
"alpha2Code": "US",
"alpha3Code": "USA",
"callingCodes": [
"1"
],
"capital": "Washington, D.C.",
"altSpellings": [
"US",
"USA",
"United States of America"
],
"region": "Americas",
"subregion": "Northern America",
"population": 321645000,
"latlng": [
38,
-97
],
"demonym": "American",
"area": 9629091,
"gini": 48,
"timezones": [
"UTC-12:00",
"UTC-11:00",
"UTC-10:00",
"UTC-09:00",
"UTC-08:00",
"UTC-07:00",
"UTC-06:00",
"UTC-05:00",
"UTC-04:00",
"UTC+10:00",
"UTC+12:00"
],
"borders": [
"CAN",
"MEX"
],
"nativeName": "United States",
"numericCode": "840",
"currencies": [
"USD",
"USN",
"USS"
],
"languages": [
"en"
],
"translations": {
"de": "Vereinigte Staaten von Amerika",
"es": "Estados Unidos",
"fr": "États-Unis",
"ja": "アメリカ合衆国",
"it": "Stati Uniti D'America"
},
"relevance": "3.5"
} ]
model class:
struct CountryModel: Codable {
let country: [MyResponse]
}
struct MyResponse: Codable {
let name: String
let capital: String
}
Manager class:
struct CountryManager {
let countryURL = "https://restcountries-v1.p.rapidapi.com/name/"
func fetchData(_ countryName: String) {
let urlString = "\(countryURL)\(countryName)"
print(urlString)
performRequest(urlString)
}
func performRequest(_ urlString: String){
if let url = URL(string: urlString){
var request = URLRequest(url:url)
request.setValue("x-rapidapi-key", forHTTPHeaderField: "myapikey")
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request) { (data, response, error) in
if let e = error {
print(e)
return
}
if let safeData = data {
self.parseJSON(safeData)
}
}
task.resume()
}
}
func parseJSON(_ countryData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode([CountryModel].self, from: countryData)
print(decodedData[0].country)
}
catch {
print(error)
}
}
}
Most of the fields are missing from your model. Here is how it should look like instead:
struct Country: Codable {
let name: String
let topLevelDomains: [String]
let alpha2Code: String
let alpha3Code: String
let callingCodes: [String]
let capital: String
let altSpellings: [String]
let region: String
let subregion: String
let population: Int
let latlng: [Int]
let demonym: String
let area: Int
let gini: Int
let timezones: [String]
let borders: [String]
let nativeName: String
let numericCode: String
let currencies: [String]
let languages: [String]
let translation: Translation
let relevance: String
}
struct Translation: Codable {
let de: String
let es: String
let fr: String
let ja: String
let it: String
}
I found out that there was a problem in my http request. I used Alamofire in the request part and I don't experience the problem anymore. The issue seemed to be related to the decoding but I don't know. I'm posting the final code if anyone experiences the same issue.
import Foundation
import Alamofire
struct CountryManager {
let countryURL = "https://restcountries-v1.p.rapidapi.com/name/"
func fetchData(_ countryName: String) {
let urlString = "\(countryURL)\(countryName)"
print(urlString)
performRequest(urlString)
}
func performRequest(_ urlString: String){
let headers: HTTPHeaders = [
"x-rapidapi-host": "restcountries-v1.p.rapidapi.com",
"x-rapidapi-key": "apices"
]
AF.request(urlString, headers: headers).responseJSON { response in
self.parseJSON(response.data!)
}
}
func parseJSON(_ countryData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(CountryAlias.self, from: countryData)
print(decodedData[0].name)
}
catch {
print(error)
}
}
}

Resources