I have this printed output from parsed JSON file:
[App.Root(fields: ["12345": App.Field(timestampValue: "2020-02-04"), "7895": App.Field(timestampValue: "2020-02-04")], createTime: "2020-02-04", updateTime: "2020-02-04")]
There is my code from UserData.swift file:
import Foundation
struct Root: Codable {
let fields: [String: Field]
let createTime, updateTime: String
}
// MARK: - Field
struct Field: Codable {
let timestampValue: String
}
How can I create variable for etc.:Id 12345?
I need to put it in tableView in this format:
ID:12345
Date:2020-02-04
next row in table view:
ID:7895
Date:2020-02-04
Many thanks for some suggestions :)
You can try
let res = try jsonDecoder.decode(Root.self, from: data)
var all = [Item]()
for (id,item) in res.fields {
all.append(Item(id:id,timestampValue:item.timestampValue))
}
struct Item {
let id,timestampValue:String
}
Then use all as table dataSource array
Related
Essentially I have been creating the following simple String/Int arrays to record info
into an array set (which of course checks for duplicates before appending) how can I record an ID alongside the string to create a JSON array?
Currently doing the following:
var myArray = Set<String>()
myArray.insert("Example1")
["Example1","Example2","Example3"]
Example of what I would like to achieve.
[{"myexample":"Example2","id":1},{"myexample":"Example3","id":2}]
You can use a struct that conforms to Codable to represent your data:
struct Model : Codable, Hashable {
var myexample: String
var id: Int
}
var mySet = Set([Model(myexample: "Example2", id: 1),
Model(myexample: "Example3", id: 2)])
mySet.insert(Model(myexample: "Example4", id: 3))
do {
let json = try JSONEncoder().encode(mySet)
print(String(data: json, encoding: .utf8)!)
} catch {
print(error)
}
I am using coinmarketcap api to fetch coin prices using the code down below. The data model Coin is also given below after the code as well as the JSON response. I get an error "The data couldn’t be read because it isn’t in the correct format." What should the correct formating look like?
'''
import Foundation
import SwiftUI
import Alamofire
class CryptoViewModel: ObservableObject {
func fetchData() {
let headers: HTTPHeaders = [
"Accepts": "application/json",
"X-CMC_PRO_API_KEY": "5dd693fc-6446-44c4-8aaa-75b1bfa4376f"
]
AF.request("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest", headers: headers).response { response in
guard let data = response.data else { return }
do {
let coins = try JSONDecoder().decode([Coin].self, from: data)
print(coins)
}
catch {
print(error.localizedDescription)
}
}
}
}
'''
'''
import SwiftUI
struct Coin: Decodable {
var slug: String?
var symbol: String?
enum CodingKeys: String, CodingKey {
case slug = "slug"
case symbol = "symbol"
}
}
'''
'''
success({
data = (
{
"circulating_supply" = 18697137;
"cmc_rank" = 1;
"date_added" = "2013-04-28T00:00:00.000Z";
id = 1;
"last_updated" = "2021-05-02T14:22:02.000Z";
"max_supply" = 21000000;
name = Bitcoin;
"num_market_pairs" = 9549;
platform = "<null>";
quote = {
USD = {
"last_updated" = "2021-05-02T14:22:02.000Z";
"market_cap" = "1063000586851.752";
"percent_change_1h" = "0.09591311";
"percent_change_24h" = "-1.05109813";
"percent_change_30d" = "-4.45794679";
"percent_change_60d" = "11.80459387";
"percent_change_7d" = "14.06195861";
"percent_change_90d" = "69.54985569999999";
price = "56853.65555441735";
"volume_24h" = "40969975368.50657";
};
};
slug = bitcoin;
symbol = BTC;
tags = (
mineable,
pow,
"sha-256",
"store-of-value",
"state-channels",
"coinbase-ventures-portfolio",
"three-arrows-capital-portfolio",
"polychain-capital-portfolio",
"binance-labs-portfolio",
"arrington-xrp-capital",
"blockchain-capital-portfolio",
"boostvc-portfolio",
"cms-holdings-portfolio",
"dcg-portfolio",
"dragonfly-capital-portfolio",
"electric-capital-portfolio",
"fabric-ventures-portfolio",
"framework-ventures",
"galaxy-digital-portfolio",
"huobi-capital",
"alameda-research-portfolio",
"a16z-portfolio",
"1confirmation-portfolio",
"winklevoss-capital",
"usv-portfolio",
"placeholder-ventures-portfolio",
"pantera-capital-portfolio",
"multicoin-capital-portfolio",
"paradigm-xzy-screener"
);
"total_supply" = 18697137;
}, ...
'''
First, I think you might want to remove the API key in your example and reset it.
Regarding your question. Your response starts with a data property. To parse this you would need start your struct there as well.
So something like this should work;
struct Coins: Decodable {
let data: [Coin]
struct Coin: Decodable {
let symbol: String
let slug: String
let quote: [String: Quote]
}
struct Quote: Decodable {
let price: Double
}
}
I'm nesting the structs to preserve the namespace. Everything is pretty related. If you ever need access to one of them on it's own you could pull them out.
Also you can omit the CodingKeys, since the key is the same as your variable name. Also I don't think you need optionals there, but I'm not completely familiar with the API.
Furthermore I think you get only 1 set of data back and not an array of coins. So I would do the following here;
let coins = try JSONDecoder().decode(Coins.self, from: data)
You are parsing an entire response as an array of coins, but the actual data, that you are interested in, is located under data key, therefore you need to add an additional layer
struct Coins: Decodable {
let coins: [Coin]
}
and decode it instead of Coin
let coins = try JSONDecoder().decode(Coins.self, from: data)
I have the following JSON object which is to be converted to an object using JSONDecoder:
{
"first_key": 3,
"image_1000x1000": "location"
}
This maps to the following Swift model:
struct Response: Decodable {
var firstKey: Int
var image1000x1000: String
}
By using JSONDecoder with the .convertFromSnakeCase option, the snake_case keys inside the JSON are transformed to camelCase by using the algorithm defined in the documentation:
This strategy follows these steps to convert JSON keys to camel-case:
Capitalize each word that follows an underscore.
Remove all underscores that aren't at the very start or end of the string.
Combine the words into a single string.
Therefore, in this case:
first_key becomes firstKey (as expected)
image_1000x1000 should become image1000x1000
However when attempting to decode this response, a keyNotFound error for the image1000x1000 key is thrown (see this live example):
let json = "{\"first_key\": 3, \"image_1000x1000\": \"location\"}".data(using: .utf8)!
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try decoder.decode(Response.self, from: json)
print(response)
} catch let e {
print(e)
}
What is incorrect about my camel case conversion of image_1000x1000, and why can’t JSONDecoder find the corresponding key?
You can see what the algorithm is expecting by running the process in reverse; use a JSONEncoder to encoder your data and inspect the output:
struct Response: Codable {
var firstKey: Int
var image1000x1000: String
}
let test = Response(firstKey: 10, image1000x1000: "secondKey" )
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
let data = try encoder.encode(test)
print(String(data: data, encoding: .utf8)!)
This will produce:
{"first_key":10,"image1000x1000":"secondKey"}
So if you have control over the JSON and can live with image1000x1000 as a key, then you're done. If not you'll have to do something like this:
struct Response: Codable {
var firstKey: Int
var image1000x1000: String
private enum CodingKeys: String, CodingKey {
case image1000x1000 = "image_1000x1000"
case firstKey = "first_key"
}
}
Another option is to implement a custom key encoding strategy. It may end up being less code. See KeyEncodingStrategy for more about this.
I am facing the same issue my json keys are iso639_1 and iso639_2. Without adding Coding key it will works.
make your variable optional
Here it's my Decodable model
struct CountryModel: Decodable{
var name: String
var borders: [String]
var region: String
var alpha2Code: String
var alpha3Code: String
var flag: String
var languages: [languages]
}
struct languages: Decodable {
var iso639_1: String?
var iso639_2: String?
var name: String
var nativeName: String
}
When I add optional to both underscore variables iso639_1 and iso639_2. Then it works fine may be due to Null Value!
Here, in your case add optional to your image1000x1000 variable. Like below
struct Response: Decodable {
var firstKey: Int
var image_1000x1000: String?
}
Hope it will works!
I have json as below.
{
"CHF": 1.0064,
"KZT": 0.0027,
"ZAR": 0.0676,
"INR": 0.0136,
"CNY": 0.1456,
"UZS": 0.0001,
"AUD": 0.7062,
"KRW": 0.0009
}
This is nothing but list of currency & their rates.
I am confused how to parse this data.
Usually I was creating Model to parse the json data as below for User data (& not for above case).
struct UserData : Decodable {
var firstName : String?
var lastName : String?
}
& while parsing I have as below.
let globalErrObj = try JSONDecoder().decode(UserData.self, from: data!)
However as this is key value data, I am confused how Model & parsing would be.
My suggestion is to decode the JSON as [String:Double] and map it to an array of a custom struct
struct Rate {
let name : String
let value : Double
}
let rates = try JSONDecoder().decode([String: Double].self, from: data!).map(Rate.init)
I am working on a native iOS application that receives data in JSON format from a web-service which we are also in control of. The plan is to change out the backend database in a bout 18 months in favor of a different platform.
With that in mind, we want to be sure that that iOS app is going to be relatively easy to adapt to the new datasource, particularly as we may change the keys used in the associative array received from the server via JSON.
There are two goals:
Create a single location for each PHP request where the keys can be modified if needed. This would avoid digging through code to find things like job["jobNumber"].
Clean up our existing code to also eliminate references like job["jobNumber"].
We are both very new to Swift with no Objective-C experience, but I am was thinking a Struct or Class would be appropriate to create references like job.jobNumber.
Should a dictionary be converted into a class or struct? Sample code representing a reusable method of taking a Dictionary<String, String> as shown below and converting it to the recommended type would be extremely helpful.
Example Dictionary:
job = {
"jobNumber" : "1234",
"jobName" : "Awards Ceremony",
"client" : "ACME Productions"
}
Desired result:
println("job name is \(job.name)")
// prints: job name is Awards Ceremony
To access it like this you need to convert your dictionary to Struct as follow:
edit/update: Swift 3.x
struct Job: CustomStringConvertible {
let number: Int
let name, client: String
init(dictionary: [String: Any]) {
self.number = dictionary["jobNumber"] as? Int ?? 0
self.name = dictionary["jobName"] as? String ?? ""
self.client = dictionary["client"] as? String ?? ""
}
var description: String {
return "Job#: " + String(number) + " - name: " + name + " - client: " + client
}
}
let dict: [String: Any] = ["jobNumber": 1234,
"jobName" : "Awards Ceremony",
"client" : "ACME Productions"]
let job = Job(dictionary: dict)
print(job.number) // 1234
print(job.name) // "Awards Ceremony"
print(job.client) // "ACME Productions"
print(job) // "Job#: 1234 - name: Awards Ceremony - client: ACME Productions"""
edit/update:
Swift 4 or later you can use JSON Codable protocol:
struct Job {
let number: Int
let name, client: String
}
extension Job: Codable {
init(dictionary: [String: Any]) throws {
self = try JSONDecoder().decode(Job.self, from: JSONSerialization.data(withJSONObject: dictionary))
}
private enum CodingKeys: String, CodingKey {
case number = "jobNumber", name = "jobName", client
}
}
extension Job: CustomStringConvertible {
var description: String {
return "Job#: " + String(number) + " - name: " + name + " - client: " + client
}
}
let dict: [String: Any] = ["jobNumber": 1234,
"jobName" : "Awards Ceremony",
"client" : "ACME Productions"]
do {
let job = try Job(dictionary: dict)
print(job.number) // 1234
print(job.name) // "Awards Ceremony"
print(job.client) // "ACME Productions"
print(job) // "Job#: 1234 - name: Awards Ceremony - client: ACME Productions\n"
} catch {
print(error)
}
Definitely a job for a struct.
1. Structs are thread-safe and don't need to be managed by ARC.
2. Some studies have found them to be about 30,000x faster to work with than classes in general.
3. Structs also provide default initializers so your code will be cleaner.
4. In this case, you don't have to worry about inheritance/subclassing.
5. The Protocol Oriented Programming paradigm recommends using structs over classes if you're able.
struct Job {
let number: Int
let name: String
let client: String
}
Initializer for free:
let newJob = Job(number: 2, name: "Honey", client: "Jeff")
Or you can create a custom initializer that takes the dictionary:
struct Job {
let number: Int
let name: String
let client: String
init(json: [String: Any]) {
self.number = Int(dictionary["jobNumber"] as? String) ?? 0
self.name = dictionary["jobName"] as? String ?? ""
self.client = dictionary["client"] as? String ?? ""
}
}
usage:
let newJob = Job(json: yourDictionary)
print(newJob.number)
// outputs: 1234
You can add an extension to Dictionary like this to get generic objects:
extension Dictionary where Key == String, Value: Any {
func object<T: Decodable>() -> T? {
if let data = try? JSONSerialization.data(withJSONObject: self, options: []) {
return try? JSONDecoder().decode(T.self, from: data)
} else {
return nil
}
}
}
and use like this on any [String: Any] dictionaries:
let object: MyDecodableStruct? = dictionary.object()
I generally make use of value classes to achieve what you want to do. In my project I do something like following:
protocol Request : class {
func toDictionary() -> [String : String]
}
protocol Response : class {
init(dictionary: [String : String])
}
class MyRequest : Request {
var var1: Int
var var2: String
//Other stuff in class...
func toDictionary() -> [String : String] {
//Convert the value to dictionary and return
}
}
class MyResponse : Response {
var var1: String
var var2: Int
//You could have nested object as members
var innerObject: MyInnerResponseClass
//Other stuff in class...
var someCalculatedProperty: String {
return //Calculate property value
}
required init(dictionary: [String : String]) {
//Initialize class from dictionary
}
}
class MyInnerResponseClass: Response {
//Class definition here...
}
For objects that could be used as request and response you could implement both of the protocols.
You need to write code for translation once, but then it could be easy to use everywhere. Also by using calculated properties you may get extra ease.
I am not sure if you could just do it out of the box in Swift. I will require reflection which is not yet very well supported by Swift. Also even if there is reflection and you come up with clever way to use to achieve what you need, it could be quite slow if the data is quite large.
My two cents about "logic". )all correct about using structs and so on...)
Do NOT keep (as many from web do..) data in dict or JSON, convert it to struct always.
a lot of efficiently, think about for example about sorting in a tableview..