Swift 4 Decodable - No value associated with key CodingKeys [duplicate] - json

This question already has answers here:
How do I use custom keys with Swift 4's Decodable protocol?
(4 answers)
Closed 3 years ago.
I'm decoding a JSON response in my Swift App, and the code used to work till it decided to stop working.
this is my json reposnse
{
"foods": [
{
"food_name": "Milk Chocolate",
"brand_name": "Snickers",
"serving_weight_grams": 41.7,
"nf_calories": 212.3,
"nf_total_fat": 11.6,
"nf_saturated_fat": 4,
"nf_total_carbohydrate": 22.7,
"nf_protein": 3.9
}
]
}
And this is the code to decode my json
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
do {
//Decode dataResponse received from a network request
let decoder = JSONDecoder()
let foods = try decoder.decode(JSONFoods.self, from: data) //Decode JSON Response Data
self.jsonfood = foods.JSONFood[0]
print(self.jsonfood!)
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
And my Structs are
struct JSONFoods: Decodable {
var JSONFood: [JSONFood]
}
struct JSONFood: Decodable{
var food_name: String
var brand_name: String
var nf_calories: Int
var nf_protein: Int
var nf_total_fat: Int
var nf_total_carbohydrate: Int
var serving_weight_grams: Int
}
And the error message I get is this
keyNotFound(CodingKeys(stringValue: "JSONFood", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "JSONFood", intValue: nil) ("JSONFood").", underlyingError: nil))
And if i get replace decode(JSONFoods.self, from: data) with decode(JSONFood.self, from: data)
I get this error message
keyNotFound(CodingKeys(stringValue: "food_name", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "food_name", intValue: nil) ("food_name").", underlyingError: nil))
I searched everywhere with no luck, any help is very appreciated

You need
struct Root: Codable {
let foods: [Food]
}
struct Food: Codable {
let foodName: String?
let brandName: String
let servingWeightGrams, nfCalories, nfTotalFat: Double
let nfSaturatedFat: Int
let nfTotalCarbohydrate, nfProtein: Double
enum CodingKeys: String, CodingKey {
case foodName = "food_name"
case brandName = "brand_name"
case servingWeightGrams = "serving_weight_grams"
case nfCalories = "nf_calories"
case nfTotalFat = "nf_total_fat"
case nfSaturatedFat = "nf_saturated_fat"
case nfTotalCarbohydrate = "nf_total_carbohydrate"
case nfProtein = "nf_protein"
}
}
First : you make JSONFood while it should be foods
Second :food_name doesn't exist in current json root so this will fail
let foods = try decoder.decode(JSONFoods.self, from: data) //Decode JSON Response Data
In case to take advantage of convertFromSnakeCase
let str = """
{"foods":[{"food_name":"Milk Chocolate","brand_name":"Snickers","serving_weight_grams":41.7,"nf_calories":212.3,"nf_total_fat":11.6,"nf_saturated_fat":4,"nf_total_carbohydrate":22.7,"nf_protein":3.9}]}
"""
do {
let res = JSONDecoder()
res.keyDecodingStrategy = .convertFromSnakeCase
let ss = try res.decode(Root.self, from:Data(str.utf8))
print(ss)
}
catch {
print(error)
}
struct Root: Codable {
let foods: [Food]
}
struct Food: Codable {
let foodName: String?
let brandName: String
let servingWeightGrams, nfCalories, nfTotalFat: Double
let nfSaturatedFat: Int
let nfTotalCarbohydrate, nfProtein: Double
}

Related

how to parse this complex nested Json

The Json is valid but I m getting nil with below code and struct for the returned json.
problem encountered:
at JSonDecoder.decode() : it returned this error msg:
keyNotFound(CodingKeys(stringValue: "items", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "items", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "items", intValue: nil) ("items").", underlyingError: nil))
How to get these a) image b) location from the Json
Thanks
here the code
func getJsonMapData(){
guard let mapUrl = URL(string: "https://xxxxxx/traffic-images") else { return }
URLSession.shared.dataTask(with: mapUrl) { (data, response, error) in
guard error == nil else { return}
guard let data = data else { return}
//- problem:
do {
let LocationArrDict = try JSONDecoder().decode([String:[Location]].self, from: data)else{
print(LocationArrDict)
} catch {
print(error)
}
}.resume()
}
//------------- return Json String:
{
"items":[
{
"timestamp":"2020-12-05T08:45:43+08:00",
"cameras":[
{
"timestamp":"2020-11-05T08:42:43+08:00",
"image":"https://xxxxxxxxx/traffic-images/2020/12/2ab06cd8-4dcf-434c-b758-804e690e57db.jpg",
"location":{
"latitude":1.29531332,
"longitude":103.871146
},
"camera_id":"1001",
"image_metadata":{
"height":240,
"width":320,
"md5":"c9686a013f3a2ed4af61260811661fc4"
}
},
{
"timestamp":"2020-11-05T08:42:43+08:00",
"image":"https://xxxxxxxxxx/traffic-images/2020/12/9f6d307e-8b05-414d-b27d-bf1414aa2cc7.jpg",
"location":{
"latitude":1.319541067,
"longitude":103.8785627
},
"camera_id":"1002",
"image_metadata":{
"height":240,
"width":320,
"md5":"78060d8fbdd241adf43a2f1ae5d252b1"
}
},
........
{
"timestamp":"2020-12-05T08:42:43+08:00",
"image":"https://xxxxxx/traffic-images/2020/12/98f64fe6-5985-4a8a-852f-0be24b0a6271.jpg",
"location":{
"latitude":1.41270056,
"longitude":103.80642712
},
"camera_id":"9706",
"image_metadata":{
"height":360,
"width":640,
"md5":"f63d54176620fa1d9896fa438b3cc753"
}
}
]
}
],
"api_info":{
"status":"healthy"
}
}
//------------ struct for the return Json result:
// MARK: - Location
struct Location: Codable {
let items: [Item]
let apiInfo: APIInfo
enum CodingKeys: String, CodingKey {
case items
case apiInfo = "api_info"
}
}
// MARK: - APIInfo
struct APIInfo: Codable {
let status: String
}
// MARK: - Item
struct Item: Codable {
let timestamp: Date
let cameras: [Camera]
}
// MARK: - Camera
struct Camera: Codable {
let timestamp: Date
let image: String
let location: LocationClass
let cameraID: String
let imageMetadata: ImageMetadata
enum CodingKeys: String, CodingKey {
case timestamp, image, location
case cameraID = "camera_id"
case imageMetadata = "image_metadata"
}
}
// MARK: - ImageMetadata
struct ImageMetadata: Codable {
let height, width: Int
let md5: String
}
// MARK: - LocationClass
struct LocationClass: Codable {
let latitude, longitude: Double
}
``
Error #1: The type to be decoded is wrong it must be Location.self.
Error #2: To decode the ISO date as Date you have to add the .iso8601 date decoding strategy.
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let locationArrDict = try decoder.decode(Location.self, from: data)
print(locationArrDict)
} catch {
print(error)
}
And you could decode the strings representing an URL directly to URL
You have to create your data models and describe them as the response you expect to receive. I will just give you a brief example based on your json how you would decode it.
First of all in order to decode a JSON using JSONDecoder your models have to conform to Decodable protocol. Then you have to create those models.
struct Item: Decodable {
let timestamp: Date
// let cameras: [...]
}
struct ApiInfo: Decodable {
enum Status: String, Decodable {
case healthy
}
let status: Status
}
struct Response: Decodable {
let items: [Item]
let apiInfo: ApiInfo
}
Then you have to configure your JSONDecoder and decode that JSON. As we can see clearly, your JSON uses snake_case naming convention and that is not the default one for JSONDecoder so you have to set it. Same also applies for the dates - it is used iso8601 standard of representation.
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .iso8601
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
Finally, you have you decode it.
do {
let response = try jsonDecoder.decode(Response.self, from: jsonData)
print(response)
} catch {
print(error)
}

Swift json decode with dynamic keys

i'm trying to decode a nested json with dynamic keys, but can't find a solution.
here's my json:
{
"available_channels": {
"1000": {
"creation_date": "1111222",
"category_id": "9"
},
"1001": {
"creation_date": "222333",
"category_id": "10"
}
}
as you can see, "1000" and "1001" are dynamique.
The models i'm using:
struct StreamsData: Codable{
let availableChannels: AvailableChannels
}
struct AvailableChannels: Codable{
let channels: [String: Channel]
}
struct Channel: Codable{
let creationDate: String
let categoryId: String
}
'StreamsData' is the root object
'AvailableChannels' is the objects containing all channels objects
'Channel' channel model
decoding the json:
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let streams = try decoder.decode(StreamsData.self, from: data)
With this code i have this error:
CodingKeys(stringValue: "availableChannels", intValue: nil)
- debugDescription : "No value associated with key CodingKeys(stringValue: \"channels\", intValue: nil) (\"channels\")."
The problem is clear, as 'AvailableChannels' is declared to have a 'channel' property, the decoder is trying to find "channels" as key for the object containing the "creation_date".
Could you help me to solve this problem, thanks.
You only need
struct StreamsData: Codable{
let availableChannels: [String: Channel]
}
struct Channel: Codable{
let creationDate,categoryId: String
}
do {
let dec = JSONDecoder()
dec.keyDecodingStrategy = .convertFromSnakeCase
let res = try dec.decode(StreamsData.self, from: data)
}
catch {
print(error)
}

How to use Swift Codable with a nested JSON structure and unknown keys

I have a nested json structure. I know the top level keys (objects) but those may or may not be present for each fetch. Each key within the objects (and the ones nested inside those) is unknown. They are dynamic.
I have tried for hours to use Codable only to use JSONSerialization instead. Before I completely lose hope, I wanted to see if anyone has a solution for this.
Here is an example of what my JSON looks like:
var jsonString =
"""
{
"someNumbers": {
"22": 6,
"22626": 0
},
"someNestedAny": {
"61": {
"browser": 2
},
"8310": {
"desktop": 2
}
},
"someNestedArray": {
"49": ["Chrome"],
"50": ["Mac OS X"],
"51": ["Mac desktop"],
"52": ["browser"],
"53": ["Chrome"]
}
}
"""
The key value pairs within each top level object (someNumbers, someNestedAny, and someNestedArray are dynamic. The keys/values within those objects are also dynamic...and so on. Each top level object is also optional.
I have tried many things, but these are what have looked the most promising (neither of them worked, tho)
struct TopLevel: Decodable {
var someNumbers: SomeNumbers?
var someNestedAny: SomeNestedAny?
var someNestedArray: SomeObjectFromNestedAny?
}
struct SomeNumbers: Decodable {
var key: String
var value: Int
}
struct SomeNestedAny: Decodable {
var key: String
var value: SomeObjectFromNestedAny
}
struct SomeObjectFromNestedAny: Decodable {
var key: String
var value: Int
}
struct SomeNestedArray: Decodable {
var key: String
var value: [String]
}
let data = jsonString.data(using: .utf8)!
do {
let result = try JSONDecoder().decode(TopLevel.self, from: data)
print(result)
} catch {
print(error)
}
Output: keyNotFound(CodingKeys(stringValue: "key", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "someNumbers", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"key\", intValue: nil) (\"key\").", underlyingError: nil))
struct TopLevel: Decodable {
var someNumbers: SomeNumbers?
var someNestedAny: SomeNestedAny?
var someNestedArray: SomeObjectFromNestedAny?
}
struct SomeNumbers: Decodable {
public var numbersObject: [String: NumberKeys]
public struct NumberKeys: Decodable {
public let key: String
public let value: Int
}
private struct NumberCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: NumberCodingKeys.self)
self.numbersObject = [String: NumberKeys]()
for key in container.allKeys {
let value = try container.decode(NumberKeys.self, forKey: NumberCodingKeys(stringValue: key.stringValue)!)
self.numbersObject[key.stringValue] = value
}
}
}
struct SomeNestedAny: Decodable {
// was going to do the same thing as above - but it didn't work
var key: String
var value: SomeObjectFromNestedAny
}
struct SomeObjectFromNestedAny: Decodable {
// was going to do the same thing as above - but it didn't work
var key: String
var value: Int
}
struct SomeNestedArray: Decodable {
// was going to do the same thing as above - but it didn't work
var key: String
var value: [String]
}
let data = jsonString.data(using: .utf8)!
do {
let result = try JSONDecoder().decode(TopLevel.self, from: data)
print(result)
} catch {
print(error)
}
Output: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "someNumbers", intValue: nil), NumberCodingKeys(stringValue: "22626", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil))
Here is what I have working now, but is really gross:
let data = jsonString.data(using: .utf8)
let json = try JSONSerialization.jsonObject(with: data!) as! [String:Any]
if let numbers = json["someNumbers"] as? [String:Any] {
for (key, value) in numbers {
print("key: \(key), value: \(value)")
}
}
if let anys = json["someNestedAny"] as? [String: Any] {
//print("tkey: \(anys)")
for (key, value) in anys {
//print("tkey2: \(key), tvalue: \(value)")
if let value = value as? [String: Any] {
let anyData = try JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
let anyJson = try JSONSerialization.jsonObject(with: anyData) as! [String: Any]
for (key2, value2) in anyJson {
print("key: \(key), object-key: \(key2), object-value: \(value2)")
}
}
}
}
if let arrays = json["someNestedArray"] as? [String: Any] {
for (key, value) in arrays {
print("key: \(key), value: \(value)")
}
}
Thanks in advance!
It's hard to tell if this solves your exact use case since I'm not sure what the purpose of the data is, and how you're anticipating using it, but both of the below solutions correctly decode your JSON into a more usable Swift object.
The simplest way is to model it exactly as the data structure you presented. For example it looks like someNumbers is an optional dictionary keyed by String, with Int values: [String: Int]?.
struct TopLevel: Decodable {
var someNumbers: [String: Int]?
var someNestedAny: [String: [String: Int]]?
var someNestedArray: [String: [String]]?
}
For a little more readability when passing objects around you can throw in some type aliases and it becomes
typealias SomeNumbers = [String: Int]
typealias SomeNestedAny = [String: [String: Int]]
typealias SomeNestedArray = [String: [String]]
struct TopLevel: Decodable {
var someNumbers: SomeNumbers?
var someNestedAny: SomeNestedAny?
var someNestedArray: SomeNestedArray?
}
To get useful things out you'll then need to call things like
topLevel.someNumbers?["22"] // 6
topLevel.someNestedAny?["8310"] // ["desktop": 2]
topLevel.someNestedAny?["8310"]?["desktop"] // 2
topLevel.someNestedArray?["52"] // ["browser"]
topLevel.someNestedArray?["52"]?[0] // "browser"
Or depending on your needs it may make more sense to loop through things
topLevel.someNestedAny?
.forEach { item in
print("|- \(item.key)")
item.value.forEach { any in
print("| |- \(any.key)")
print("| | |- \(any.value)")
}
}
// |- 61
// | |- browser
// | | |- 2
// |- 8310
// | |- desktop
// | | |- 2

How to properly decode nested JSON objects with Swift structs

Intent:
Receive cryptocurrency price data via Coinmarketcap API, decode it into custom structs in SWIFT and potentially store that data in a database (either CoreData or SQLite).
Context:
I am receiving the following error on JSONDecoder().decode:
Error serializing json: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "status", intValue: nil), _DictionaryCodingKey(stringValue: "credit_count", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil))
Questions:
How to properly interpret that error? What am I decoding wrong?
Is the data I am receiving correctly formatted? Doesn't look like
proper JSON.
The code:
import UIKit
import PlaygroundSupport
// Defining structures
struct RootObject: Decodable {
let status: [String: StatusObject?]
let data: DataObject?
}
struct StatusObject: Decodable {
let credit_count: Int?
let elapsed: Int?
let error_code: Int?
let timestamp: String?
}
struct DataObject: Decodable {
let amount: Int?
let id: Int?
let last_updated: String?
let name: String?
let quote: [QuoteObject]?
let symbol: String?
}
struct QuoteObject: Decodable {
let usd: String?
}
struct usdObject: Decodable {
let last_updated: String?
let price: String?
}
//Configuring URLSession
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = ["X-CMC_PRO_API_KEY": "<removed>",
"Accept": "application/json",
"Accept-Encoding": "deflate, gzip"]
let session = URLSession(configuration: config)
let url = URL(string: "https://sandbox-api.coinmarketcap.com/v1/tools/price-conversion?convert=USD&amount=1&symbol=BTC")!
//Making and handling a request
let task = session.dataTask(with: url) { data, response, error in
guard error == nil else {
print ("error: \(error!)")
return
}
guard let content = data else {
print("No data")
return
}
//Serializing and displaying the received data
guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any]
else {
print("Not containing JSON")
return
}
print(json)
//Trying to decode
do {
let prices = try JSONDecoder().decode(RootObject.self, from: data!)
print(prices)
} catch let decodeError {
print("Error serializing json:", decodeError)
}
}
task.resume()
The data response and the error:
["status": {
"credit_count" = 1;
elapsed = 6;
"error_code" = 0;
"error_message" = "<null>";
timestamp = "2019-02-16T11:10:22.147Z";
}, "data": {
amount = 1;
id = 1;
"last_updated" = "2018-12-22T06:08:23.000Z";
name = Bitcoin;
quote = {
USD = {
"last_updated" = "2018-12-22T06:08:23.000Z";
price = "3881.88864625";
};
};
symbol = BTC;
}]
Error serializing json: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "status", intValue: nil), _DictionaryCodingKey(stringValue: "credit_count", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil))
Edit 1:
Properly serialized JSON:
{
"status": {
"timestamp": "2019-02-16T18:54:05.499Z",
"error_code": 0,
"error_message": null,
"elapsed": 6,
"credit_count": 1
},
"data": {
"id": 1,
"symbol": "BTC",
"name": "Bitcoin",
"amount": 1,
"last_updated": "2018-12-22T06:08:23.000Z",
"quote": {
"USD": {
"price": 3881.88864625,
"last_updated": "2018-12-22T06:08:23.000Z"
}
}
}
}
There are a lot of issues in the structs.
The main issue is that the value for data is a dictionary which is decoded into a struct rather than into another dictionary. Other issues are that the type of id is String and price is Double.
APIs like Coinmarketcap send reliable data so don't declare everything as optional. Remove the question marks.
The structs below are able to decode the JSON. The quotes are decoded into a dictionary because the keys change. Add the .convertFromSnakeCase key decoding strategy to get camelCased keys. The dates are decoded as Date by adding an appropriate date decoding strategy.
I removed all those redundant ...Object occurrences except DataObject because the Data struct already exists.
struct Root: Decodable {
let status: Status
let data: DataObject
}
struct Status: Decodable {
let creditCount: Int
let elapsed: Int
let errorCode: Int
let timestamp: Date
}
struct DataObject: Decodable {
let amount: Int
let id: String
let lastUpdated: Date
let name: String
let quote: [String:Quote]
let symbol: String
}
struct Quote: Decodable {
let lastUpdated: Date
let price: Double
}
//Trying to decode
do {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let result = try decoder.decode(Root.self, from: data!)
let quotes = result.data.quote
for (symbol, quote) in quotes {
print(symbol, quote.price)
}
} catch {
print(error)
}

URLSession Type Mismatch Error

I'm getting an Type Mismatch error when trying to parse json with Swift jSon Decoder.
I just can't find a way to parse it normally.
The Error:
typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath:
[CodingKeys(stringValue: "data", intValue: nil),
CodingKeys(stringValue: "itemArr", intValue: nil),
CodingKeys(stringValue: "price", intValue: nil)], debugDescription:
"Expected to decode String but found a number instead.",
underlyingError:
nil))
The Decoder Code:
func getDealDetails(id : String ,completion : #escaping ()->())
{
let jsonUrl = "https://androidtest.inmanage.com/api/1.0/android/getDeal_\(id).txt"
guard let url = URL(string: jsonUrl) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do
{
let deal = try JSONDecoder().decode(ResultDetails.self, from: data)
AppManager.shared.dealToShow = deal.data.itemArr
}catch
{
print("There's an error: \(error)")
}
completion()
}.resume()
}
}
And The Classes:
First:
class ResultDetails : Decodable
{
let data : DataDetails
init(data : DataDetails) {
self.data = data
}
}
Second:
class DataDetails : Decodable
{
let itemArr: ItemArr
init(itemArr: ItemArr) {
self.itemArr = itemArr
}
}
And Third:
class ItemArr : Decodable
{
let id, title, price, description: String
let image: String
let optionsToShow: Int
let gps: Gps
let website, phone: String
init(id: String, title: String, price: String, description: String, image: String, optionsToShow: Int, gps: Gps, website: String, phone: String) {
self.id = id
self.title = title
self.price = price
self.description = description
self.image = image
self.optionsToShow = optionsToShow
self.gps = gps
self.website = website
self.phone = phone
}
I think I tried everything for the last 6 hours to fix it, please help!
EDIT:
I put a wrong third class. now it's the right one
The error message is wrong. According to the JSON link and your classes it's supposed to be
CodingKeys(stringValue: "price", intValue: nil)], debugDescription:
Expected to decode Int but found a string/data instead.,
However it's going to tell you exactly what's wrong: The value for key price is a String rather than an Int.
You have to read the JSON carefully. The format is very simple. For example everything in double quotes is String, there is no exception.
Your data structure is too complicated. Use structs and drop the initializers. By the way there is a typo ItemsArr vs. ItemArr and there is no key orderNum.
The key image can be decoded as URL. This is sufficient
struct ResultDetails : Decodable {
let data : DataDetails
}
struct DataDetails : Decodable {
let itemArr: ItemArr
}
struct ItemArr : Decodable {
let id, title: String
let price: String
let image: URL
// let orderNum: Int
}
Specify the CodingKeys only if you want to map the keys. In your case you can even omit the CodingKeys, use theconvertFromSnakeCase strategy instead.
decoder.keyDecodingStrategy = .convertFromSnakeCase
Yes you have an error
let price: Int should be let price: String because it string in Json "price": "896" -- > String
Example for Int "optionsToShow":1 --> this is Int
Here is complete code you can use
import Foundation
struct ResultDetails: Codable {
let data: DataDetails
}
struct DataDetails: Codable {
let itemArr: ItemArr
}
struct ItemArr: Codable {
let id, title, price, description: String
let image: String
let optionsToShow: Int
let gps: Gps
let website, phone: String
}
struct Gps: Codable {
let lat, lon: String
}
// MARK: Convenience initializers
extension ResultDetails {
init(data: Data) throws {
self = try JSONDecoder().decode(ResultDetails.self, from: data)
}
init(_ json: String, using encoding: String.Encoding = .utf8) throws {
guard let data = json.data(using: encoding) else {
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
}
try self.init(data: data)
}
func jsonData() throws -> Data {
return try JSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}