Swift Codable: Decode different array of items with same root objects - json

I'm currently trying to decode JSON which looks like this:
{
"result": {
"success": true,
"items": [
{
"timeEntryID": "1",
"start": "1519558200",
"end": "1519563600",
"customerName": "Test-Customer",
"projectName": "Test-Project",
"description": "Entry 1",
},
{
"timeEntryID": "2",
"start": "1519558200",
"end": "1519563600",
"customerName": "Test-Customer",
"projectName": "Test-Project",
"description": "Entry 2",
}
],
"total": "2"
},
"id": "1"
}
The decoding process for this specific type of JSON is pretty simple. I just need something like this:
struct ResponseKeys: Decodable {
let result: ResultKeys
struct ResultKeys: Decodable {
let success: Bool
let items: [Item]
}
}
Now the problem I'm facing is that every response of the server has the same structure as the above JSON but with different item types. So sometimes it is let items: [Item] but it could also be let items: [User] if I make a call to the User endpoint.
Because it would be an unnecessary duplication of code if I would write the above swift code for every endpoint with just the modification of the items array, I created a custom decoder:
enum KimaiAPIResponseKeys: String, CodingKey {
case result
enum KimaiResultKeys: String, CodingKey {
case success
case items
}
}
struct Activity: Codable {
let id: Int
let description: String?
let customerName: String
let projectName: String
let startDateTime: Date
let endDateTime: Date
enum CodingKeys: String, CodingKey {
case id = "timeEntryID"
case description
case customerName
case projectName
case startDateTime = "start"
case endDateTime = "end"
}
}
extension Activity {
init(from decoder: Decoder) throws {
let resultContainer = try decoder.container(keyedBy: KimaiAPIResponseKeys.self)
let itemsContainer = try resultContainer.nestedContainer(keyedBy: KimaiAPIResponseKeys.KimaiResultKeys.self, forKey: .result)
let activityContainer = try itemsContainer.nestedContainer(keyedBy: Activity.CodingKeys.self, forKey: .items)
id = Int(try activityContainer.decode(String.self, forKey: .id))!
description = try activityContainer.decodeIfPresent(String.self, forKey: .description)
customerName = try activityContainer.decode(String.self, forKey: .customerName)
projectName = try activityContainer.decode(String.self, forKey: .projectName)
startDateTime = Date(timeIntervalSince1970: Double(try activityContainer.decode(String.self, forKey: .startDateTime))!)
endDateTime = Date(timeIntervalSince1970: Double(try activityContainer.decode(String.self, forKey: .endDateTime))!)
}
}
The decoder works perfectly if "items" does only contain a single object and not an array:
{
"result": {
"success": true,
"items":
{
"timeEntryID": "2",
"start": "1519558200",
"end": "1519563600",
"customerName": "Test-Customer",
"projectName": "Test-Project",
"description": "Entry 2",
},
"total": "2"
},
"id": "1"
}
If items is an array I get the following error:
typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [__lldb_expr_151.KimaiAPIResponseKeys.result], debugDescription: "Expected to decode Dictionary but found an array instead.", underlyingError: nil))
I just cannot figure out how to modify my decoder to work with an array of items. I created a Playground file with the working and not working version of the JSON. Please take a look and try it out: Decodable.playground
Thank you for your help!

My suggestion is to decode the dictionary/dictionaries for items separately
struct Item : Decodable {
enum CodingKeys: String, CodingKey {
case id = "timeEntryID"
case description, customerName, projectName
case startDateTime = "start"
case endDateTime = "end"
}
let id: Int
let startDateTime: Date
let endDateTime: Date
let customerName: String
let projectName: String
let description: String?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = Int(try container.decode(String.self, forKey: .id))!
description = try container.decodeIfPresent(String.self, forKey: .description)
customerName = try container.decode(String.self, forKey: .customerName)
projectName = try container.decode(String.self, forKey: .projectName)
startDateTime = Date(timeIntervalSince1970: Double(try container.decode(String.self, forKey: .startDateTime))!)
endDateTime = Date(timeIntervalSince1970: Double(try container.decode(String.self, forKey: .endDateTime))!)
}
}
And in Activity use a conditional initializer, it provides it's own do catch block. First it tries to decode a single item and assigns the single item as array to the property. If it fails it decodes an array.
enum KimaiAPIResponseKeys: String, CodingKey {
case result, id
enum KimaiResultKeys: String, CodingKey {
case success
case items
}
}
struct Activity: Decodable {
let id: String
let items: [Item]
}
extension Activity {
init(from decoder: Decoder) throws {
let rootContainer = try decoder.container(keyedBy: KimaiAPIResponseKeys.self)
id = try rootContainer.decode(String.self, forKey: .id)
let resultContainer = try rootContainer.nestedContainer(keyedBy: KimaiAPIResponseKeys.KimaiResultKeys.self, forKey: .result)
do {
let item = try resultContainer.decode(Item.self, forKey: .items)
items = [item]
} catch {
items = try resultContainer.decode([Item].self, forKey: .items)
}
}
}

You Can Use Generics, It's a neat way to deal with this situation.
struct MainClass<T: Codable>: Codable {
let result: Result<T>
let id: String
}
struct Result <T: Codable>: Codable {
let success: Bool
let items: [T]
let total: String
}
and here you will get the items
let data = Data()
let decoder = JSONDecoder()
let modelObjet = try! decoder.decode(MainClass<User>.self, from: data)
let users = modelObjet.result.items
In my opinion, Generics is the best way to handle the duplication of code like this situations.

Related

How to properly process the received JSON and output information from it?

tell me how to process this JSON file. I need to display the Group array to the screen as a list. I just recently started to understand SWIFT, so I would be glad to receive any help.
I've tried writing code like this to parse JSON:
struct Post: Decodable{
var id: String?
var name: String?
var img: String?
var idproduct: Int?
var nameproduct: String?
var textproduct: String?
var priceproduct: Int?
var unitproduct: String?
var indexproduct: String?
var imgproduct: String?
var groupsproduct: [String] = []
enum group: String, CodingKey {
case id
case name
case img
}
enum Product: String, CodingKey {
case id
case name
case price
case unit
case index
case img
case group
case text
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: group.self)
self.id = try? container.decode(String.self, forKey: .id)
self.name = try? container.decode(String.self, forKey: .name)
self.img = try? container.decode(String.self, forKey: .img)
let container2 = try decoder.container(keyedBy: Product.self)
self.idproduct = try? container2.decode(Int.self, forKey: .id)
self.nameproduct = try? container2.decode(String.self, forKey: .name)
self.textproduct = try? container2.decode(String.self, forKey: .text)
self.priceproduct = try? container2.decode(Int.self, forKey: .price)
self.unitproduct = try? container2.decode(String.self, forKey: .unit)
self.indexproduct = try? container2.decode(String.self, forKey: .index)
self.imgproduct = try? container2.decode(String.self, forKey: .img)
self.groupsproduct = try! container2.decode([String].self, forKey: .group)
}
}
Here is the JSON I need to parse:
{
"id": "abs",
"name": "Testname",
"img": ""
}
],
"goods": [
{
"id": 1000,
"name": "test",
"text": "",
"price": 75,
"unit": "",
"index": "",
"img": "",
"groups": [
""
]
}
}
P.S. I am using SwiftUI 2

In Swift: How do I decode this JSON (which has variable keys)?

There is an API that supplies JSON data that I would like to use. I've given a summary of the JSON below. At the top level, the key to each record is a unique ID that matches the ID in the record itself. These keys are integers in quotes (starting at 1, unsorted and probably not contiguous).
Reading the JSON isn't a problem. What is the Codable "Response" struct required to receive the data?
if let response = try? JSONDecoder().decode(Response.self, from: data)
The JSON
{
"2546": {
"id": "2546",
"title": "Divis and the Black Mountain"
},
"1": {
"id": "1",
"title": "A la Ronde"
},
"2": {
"id": "2",
"title": "Aberconwy House"
}
}
I had this once also, looks like whoever created this endpoint doesn't really understand how JSON works...
try this out and then just return response.values so you have a list of items
struct Item: Codable {
let id, title: String
}
typealias Response = [String: Item]
Use a more dynamic version of CodingKey. You can read more about it here: https://benscheirman.com/2017/06/swift-json/
Check the section "Dynamic Coding Keys"
The Codable type struct Response should be,
struct Response: Decodable {
let id: String
let title: String
}
Now, parse the json data using [String:Response] instead of just Response like so,
do {
let response = try JSONDecoder().decode([String:Response].self, from: data)
print(response) //["1": Response(id: "1", title: "A la Ronde"), "2546": Response(id: "2546", title: "Divis and the Black Mountain"), "2": Response(id: "2", title: "Aberconwy House")]
} catch {
print(error)
}
You should implement a custom CodingKey, something like that:
struct MyResponse {
struct MyResponseItemKey: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int? { return nil }
init?(intValue: Int) { return nil }
static let id = MyResponseItemKey(stringValue: "id")!
static let title = MyResponseItemKey(stringValue: "title")!
}
struct MyResponseItem {
let id: String
let subItem: MyResponseSubItem
}
struct MyResponseSubItem {
let id: String
let title: String
}
let responseItems: [MyResponseItem]
}
Not sure if the key of each item and the value of id are always equal, that's why there are 2 IDs in MyResponse.
And, of course, MyResponse should conform to Codable:
extension MyResponse: Codable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MyResponseItemKey.self)
responseItems = try container.allKeys.map { key in
let containerForKey = try container.nestedContainer(keyedBy: MyResponseItemKey.self, forKey: key)
let id = try containerForKey.decode(String.self, forKey: .id)
let title = try containerForKey.decode(String.self, forKey: .title)
return MyResponseItem(id: key.stringValue, subItem: MyResponseSubItem(id: id, title: title))
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: MyResponseItemKey.self)
for responseItem in responseItems {
if let key = MyResponseItemKey(stringValue: responseItem.id) {
var subItemContainer = container.nestedContainer(keyedBy: MyResponseItemKey.self, forKey: key)
try subItemContainer.encode(responseItem.subItem.id, forKey: .id)
try subItemContainer.encode(responseItem.subItem.title, forKey: .title)
}
}
}
}
This is how you can use MyResponse:
let jsonString = """
{
"2546": {
"id": "2546",
"title": "Divis and the Black Mountain"
},
"1": {
"id": "1",
"title": "A la Ronde"
},
"2": {
"id": "2",
"title": "Aberconwy House"
}
}
"""
if let dataForJSON = jsonString.data(using: .utf8),
let jsonDecoded = try? JSONDecoder().decode(MyResponse.self, from: dataForJSON) {
print(jsonDecoded.responseItems.first ?? "")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
if let dataFromJSON = try? encoder.encode(jsonDecoded) {
let jsonEncoded = String(data: dataFromJSON, encoding: .utf8)
print(jsonEncoded ?? "")
}
}

decoding complicated JSON objects in Swift

I'm totally new to development. Tried to find solution but they don't help me. I need do decode next JSON data:
{
"page": {
"currentPage": 1,
"batchSize": 400,
"totalItems": "23"
},
"items": [
{
"id": "b435a598-421c-4812-a3a9-773c47864558",
"firstname": "\u041d\u0410\u0422\u0410\u041b\u0406\u042f \u0412\u0406\u041a\u0422\u041e\u0420\u0406\u0412\u041d\u0410",
"lastname": "\u0413\u0423\u041d\u042c\u041a\u041e",
"placeOfWork": "\u0437\u0430\u0441\u0442\u0443\u043f\u043d\u0438\u043a \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0430-\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0438\u043a \u0432\u0456\u0434\u0434\u0456\u043b\u0443 \u0441\u043e\u0446\u0456\u0430\u043b\u044c\u043d\u043e\u0457 \u0440\u043e\u0431\u043e\u0442\u0438 ",
"position": "",
"linkPDF": "https://public.nazk.gov.ua/storage/documents/pdf/b/4/3/5/b435a598-421c-4812-a3a9-773c47864558.pdf"
},
......
]
}
I tried this code:
struct Declarant: Codable {
var id: String
var firstname: String
var lastname: String
var placeOfWork: String
var position: String
var linkPDF: String
enum CodingKeys: String, CodingKey {
case id
case firstname
case lastname
case placeOfWork
case position
case linkPDF
}
init(from decoder: Decoder) throws {
let valueContainer = try decoder.container(keyedBy: CodingKeys.self)
self.id = try valueContainer.decode(String.self, forKey: CodingKeys.id)
self.firstname = try valueContainer.decode(String.self, forKey: CodingKeys.firstname)
self.lastname = try valueContainer.decode(String.self, forKey: CodingKeys.lastname)
self.placeOfWork = try valueContainer.decode(String.self, forKey: CodingKeys.placeOfWork)
self.position = try valueContainer.decode(String.self, forKey: CodingKeys.position)
self.linkPDF = try valueContainer.decode(String.self, forKey: CodingKeys.linkPDF)
}
}
struct DeclarationInfo: Codable {
let items: [Declarant]
enum CodingKeys: String, CodingKey {
case items
}
init(from decoder: Decoder) throws {
let valueContainer = try decoder.container(keyedBy: CodingKeys.self)
self.items = [try valueContainer.decode(Declarant.self, forKey: CodingKeys.items)]
}
}
...
let dataTask = URLSession.shared.dataTask(with: url) {
(data, response, error) in
let jsonDecoder = JSONDecoder()
print("Trying to decode data...")
if let data = data, let declarationInfo = try? jsonDecoder.decode(DeclarationInfo.self, from: data) {
completion(declarationInfo)
print(declarationInfo)
} else {
print("Either no data was returned, or data was not properly decoded.")
completion(nil)
}
}
and getting
Either no data was returned, or data was not properly decoded.
Where is the mistake?
self.items = [try valueContainer.decode(Declarant.self, forKey: CodingKeys.items)]
Should be
self.items = try valueContainer.decode( [ Declarant ].self, forKey: CodingKeys.items )
Full code:
import UIKit
struct Declarant: Codable {
var id: String
var firstname: String
var lastname: String
var placeOfWork: String
var position: String
var linkPDF: String
}
struct DeclarationInfo: Codable {
let items: [Declarant]
}
let json = """
{ \"page\" : {
\"currentPage\" : 1
, \"batchSize\" : 400
, \"totalItems\" : "23"
}
, \"items\" : [
{ \"id\" : "b435a598-421c-4812-a3a9-773c47864558 1"
, \"firstname\" : "The first name 1"
, \"lastname\" : "The last name 1"
, \"placeOfWork\" : "The placeofWork 1"
, \"position\" : "The position 1"
, \"linkPDF\" : "https://public.nazk.gov.ua/storage/documents/pdf/b/4/3/5/b435a598-421c-4812-a3a9-773c47864558.pdf"
}
, { \"id\" : "b435a598-421c-4812-a3a9-773c47864558 2"
, \"firstname\" : "The first name 2"
, \"lastname\" : "The last name 2"
, \"placeOfWork\" : "The placeofWork 2"
, \"position\" : "The position 2"
, \"linkPDF\" : "https://public.nazk.gov.ua/storage/documents/pdf/b/4/3/5/b435a598-421c-4812-a3a9-773c47864558.pdf"
}
]
}
"""
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let jsonDecoder = JSONDecoder()
print("Trying to decode data...")
if let data = json.data(using: .utf8),
let declarationInfo = try? jsonDecoder.decode(DeclarationInfo.self, from: data) {
print(declarationInfo)
} else {
print("Either no data was returned, or data was not properly decoded.")
}
}
}
Result in console:
Trying to decode data...
DeclarationInfo(items: [Dec.Declarant(id: "b435a598-421c-4812-a3a9-773c47864558 1", firstname: "The first name
1", lastname: "The last name 1", placeOfWork: "The placeofWork 1",
position: "The position 1", linkPDF:
"https://public.nazk.gov.ua/storage/documents/pdf/b/4/3/5/b435a598-421c-4812-a3a9-773c47864558.pdf"),
Dec.Declarant(id: "b435a598-421c-4812-a3a9-773c47864558 2", firstname:
"The first name 2", lastname: "The last name 2", placeOfWork: "The
placeofWork 2", position: "The position 2", linkPDF:
"https://public.nazk.gov.ua/storage/documents/pdf/b/4/3/5/b435a598-421c-4812-a3a9-773c47864558.pdf")])

Decoding nested object swift4 Codable [duplicate]

This question already has answers here:
Swift 4 JSON Decodable with multidimensional and multitype array
(5 answers)
Closed 5 years ago.
The API send me this json :
{
"name": "John Doe",
"details": [{
"name": "exampleString"
}, {
"name": [1, 2, 3]
}]
}
The problem here is that the details array have two dictionary of different value type.
how decode this json in model using the decodable protocol of swift4 ?
I don't recommend that you structure your JSOn with heterogenous types; in theis case details.name can be either a string or an array of Int. While you can do this is Swift, its kind of messy since its a statically typed language by default. In the event you can't change your JSON to something cleaner here a playground showing is how you opt into dynamic behavior with Any.
//: Playground - noun: a place where people can play
import PlaygroundSupport
import UIKit
let json = """
{
"name": "John Doe",
"details": [{
"name": "exampleString"
}, {
"name": [1, 2, 3]
}]
}
"""
struct Detail {
var name: Any?
var nameString: String? {
return name as? String
}
var nameIntArray: [Int]? {
return name as? [Int]
}
enum CodingKeys: CodingKey {
case name
}
}
extension Detail: Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if let string = name as? String {
try container.encode(string, forKey: .name)
}
if let array = name as? [Int] {
try container.encode(array, forKey: .name)
}
}
}
extension Detail: Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if let string = try? values.decode(String.self, forKey: .name) {
name = string
} else if let array = try? values.decode(Array<Int>.self, forKey: .name) {
name = array
}
}
}
struct Record: Codable {
var name: String
var details: [Detail]
}
let jsonDecoder = JSONDecoder()
let record = try! jsonDecoder.decode(Record.self, from: json.data(using: .utf8)!)
print("\(record.details.first!.name!) is of type: \(type(of:record.details.first!.name!))")
print("\(record.details.last!.name!) is of type: \(type(of:record.details.last!.name!))")
the output is:
exampleString is of type: String
[1, 2, 3] is of type: Array<Int>

Swift 4 Decodable with keys not known until decoding time

How does the Swift 4 Decodable protocol cope with a dictionary containing a key whose name is not known until runtime? For example:
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
}
]
Here we have an array of dictionaries; the first has keys categoryName and Trending, while the second has keys categoryName and Comedy. The value of the categoryName key tells me the name of the second key. How do I express that using Decodable?
The key is in how you define the CodingKeys property. While it's most commonly an enum it can be anything that conforms to the CodingKey protocol. And to make dynamic keys, you can call a static function:
struct Category: Decodable {
struct Detail: Decodable {
var category: String
var trailerPrice: String
var isFavorite: Bool?
var isWatchlist: Bool?
}
var name: String
var detail: Detail
private struct CodingKeys: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }
static let name = CodingKeys.make(key: "categoryName")
static func make(key: String) -> CodingKeys {
return CodingKeys(stringValue: key)!
}
}
init(from coder: Decoder) throws {
let container = try coder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.detail = try container.decode([Detail].self, forKey: .make(key: name)).first!
}
}
Usage:
let jsonData = """
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourite": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourite": null,
"isWatchlist": null
}
]
}
]
""".data(using: .utf8)!
let categories = try! JSONDecoder().decode([Category].self, from: jsonData)
(I changed isFavourit in the JSON to isFavourite since I thought it was a mispelling. It's easy enough to adapt the code if that's not the case)
You can write a custom struct that functions as a CodingKeys object, and initialize it with a string such that it extracts the key you specified:
private struct CK : CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
Thus, once you know what the desired key is, you can say (in the init(from:) override:
let key = // whatever the key name turns out to be
let con2 = try! decoder.container(keyedBy: CK.self)
self.unknown = try! con2.decode([Inner].self, forKey: CK(stringValue:key)!)
So what I ended up doing is making two containers from the decoder — one using the standard CodingKeys enum to extract the value of the "categoryName" key, and another using the CK struct to extract the value of the key whose name we just learned:
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: CodingKeys.self)
self.categoryName = try! con.decode(String.self, forKey:.categoryName)
let key = self.categoryName
let con2 = try! decoder.container(keyedBy: CK.self)
self.unknown = try! con2.decode([Inner].self, forKey: CK(stringValue:key)!)
}
Here, then, is my entire Decodable struct:
struct ResponseData : Codable {
let categoryName : String
let unknown : [Inner]
struct Inner : Codable {
let category : String
let trailerPrice : String
let isFavourit : String?
let isWatchList : String?
}
private enum CodingKeys : String, CodingKey {
case categoryName
}
private struct CK : CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: CodingKeys.self)
self.categoryName = try! con.decode(String.self, forKey:.categoryName)
let key = self.categoryName
let con2 = try! decoder.container(keyedBy: CK.self)
self.unknown = try! con2.decode([Inner].self, forKey: CK(stringValue:key)!)
}
}
And here's the test bed:
let json = """
[
{
"categoryName": "Trending",
"Trending": [
{
"category": "Trending",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
},
{
"categoryName": "Comedy",
"Comedy": [
{
"category": "Comedy",
"trailerPrice": "",
"isFavourit": null,
"isWatchlist": null
}
]
}
]
"""
let myjson = try! JSONDecoder().decode(
[ResponseData].self,
from: json.data(using: .utf8)!)
print(myjson)
And here's the output of the print statement, proving that we've populated our structs correctly:
[JustPlaying.ResponseData(
categoryName: "Trending",
unknown: [JustPlaying.ResponseData.Inner(
category: "Trending",
trailerPrice: "",
isFavourit: nil,
isWatchList: nil)]),
JustPlaying.ResponseData(
categoryName: "Comedy",
unknown: [JustPlaying.ResponseData.Inner(
category: "Comedy",
trailerPrice: "",
isFavourit: nil,
isWatchList: nil)])
]
Of course in real life we'd have some error-handling, no doubt!
EDIT Later I realized (in part thanks to CodeDifferent's answer) that I didn't need two containers; I can eliminate the CodingKeys enum, and my CK struct can do all the work! It is a general purpose key-maker:
init(from decoder: Decoder) throws {
let con = try! decoder.container(keyedBy: CK.self)
self.categoryName = try! con.decode(String.self, forKey:CK(stringValue:"categoryName")!)
let key = self.categoryName
self.unknown = try! con.decode([Inner].self, forKey: CK(stringValue:key)!)
}
Here's what I eventually came up for this json:
let json = """
{
"BTC_BCN":{
"last":"0.00000057",
"percentChange":"0.03636363",
"baseVolume":"47.08463318"
},
"BTC_BELA":{
"last":"0.00001281",
"percentChange":"0.07376362",
"baseVolume":"5.46595029"
}
}
""".data(using: .utf8)!
We make such a structure:
struct Pair {
let name: String
let details: Details
struct Details: Codable {
let last, percentChange, baseVolume: String
}
}
then decode:
if let pairsDictionary = try? JSONDecoder().decode([String: Pair.Details].self, from: json) {
var pairs: [Pair] = []
for (name, details) in pairsDictionary {
let pair = Pair(name: name, details: details)
pairs.append(pair)
}
print(pairs)
}
It is also possible to call not pair.details.baseVolume, but pair.baseVolume:
struct Pair {
......
var baseVolume: String { return details.baseVolume }
......
Or write custom init:
struct Pair {
.....
let baseVolume: String
init(name: String, details: Details) {
self.baseVolume = details.baseVolume
......