Hi
i did some searches in the internet but i doesnt found an usefull sample code where my json anser will parsed. i found on apple an similar answer in json and then i adapt it. but the code seems not to work...
xcode doesnt give me an usefull error if i run it...
here is my code:
//
// ViewController.swift
// test-api-calls
//
// Copyright © 2017 Producer. All rights reserved.
//
import UIKit
import Foundation
struct CurrencyStore {
var name: String
var currencies: [Currency]
struct Currency: Codable{
var symbol1: String
var maxPrice: Float
var minPrice: Float
}
}
let jsonvar = """
{"e":"currency_limits","ok":"ok","data":{"pairs":[{"symbol1":"BTC","symbol2":"USD","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":30,"minPrice":"100","maxPrice":"35000"},{"symbol1":"ETH","symbol2":"USD","minLotSize":0.1,"minLotSizeS2":2.5,"maxLotSize":1000,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"BCH","symbol2":"USD","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":30,"minPrice":"50","maxPrice":"5128"},{"symbol1":"BTG","symbol2":"USD","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"2.5","maxPrice":"2048"},{"symbol1":"DASH","symbol2":"USD","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"XRP","symbol2":"USD","minLotSize":10,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"0.005","maxPrice":"50"},{"symbol1":"ZEC","symbol2":"USD","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"BTC","symbol2":"EUR","minLotSize":0.01,"minLotSizeS2":2.2,"maxLotSize":30,"minPrice":"100.00","maxPrice":"35000"},{"symbol1":"ETH","symbol2":"EUR","minLotSize":0.1,"minLotSizeS2":2.2,"maxLotSize":null,"minPrice":"2","maxPrice":"1024"},{"symbol1":"BCH","symbol2":"EUR","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"50","maxPrice":"5128"},{"symbol1":"BTG","symbol2":"EUR","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"2.5","maxPrice":"2048"},{"symbol1":"DASH","symbol2":"EUR","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"XRP","symbol2":"EUR","minLotSize":10,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"0.005","maxPrice":"50"},{"symbol1":"ZEC","symbol2":"EUR","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"BTC","symbol2":"GBP","minLotSize":0.01,"minLotSizeS2":2.2,"maxLotSize":null,"minPrice":"100","maxPrice":"30000"},{"symbol1":"ETH","symbol2":"GBP","minLotSize":0.1,"minLotSizeS2":2.2,"maxLotSize":null,"minPrice":"50","maxPrice":"5128"},{"symbol1":"BCH","symbol2":"GBP","minLotSize":0.01,"minLotSizeS2":2.5,"maxLotSize":null,"minPrice":"50","maxPrice":"4096"},{"symbol1":"DASH","symbol2":"GBP","minLotSize":0.01,"minLotSizeS2":2.2,"maxLotSize":null,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"ZEC","symbol2":"GBP","minLotSize":0.01,"minLotSizeS2":2.2,"maxLotSize":null,"minPrice":"2.5","maxPrice":"1024"},{"symbol1":"BTC","symbol2":"RUB","minLotSize":0.01,"minLotSizeS2":150,"maxLotSize":null,"minPrice":"10000","maxPrice":"2000000"},{"symbol1":"ETH","symbol2":"BTC","minLotSize":0.1,"minLotSizeS2":0.01,"maxLotSize":1000,"minPrice":"0.001","maxPrice":"2"},{"symbol1":"BCH","symbol2":"BTC","minLotSize":0.1,"minLotSizeS2":0.01,"maxLotSize":30,"minPrice":"0.005","maxPrice":"2"},{"symbol1":"BTG","symbol2":"BTC","minLotSize":0.01,"minLotSizeS2":0.001,"maxLotSize":null,"minPrice":"0.005","maxPrice":"2"},{"symbol1":"DASH","symbol2":"BTC","minLotSize":0.01,"minLotSizeS2":0.001,"maxLotSize":null,"minPrice":"0.005","maxPrice":"2"},{"symbol1":"XRP","symbol2":"BTC","minLotSize":40,"minLotSizeS2":0.001,"maxLotSize":null,"minPrice":"0.000003","maxPrice":"0.0005"},{"symbol1":"ZEC","symbol2":"BTC","minLotSize":0.01,"minLotSizeS2":0.001,"maxLotSize":null,"minPrice":"0.005","maxPrice":"2"},{"symbol1":"GHS","symbol2":"BTC","minLotSize":1,"minLotSizeS2":0.01,"maxLotSize":null,"minPrice":"0.00001","maxPrice":"0.01"}]}}
""".data(using: .utf8)!
struct CurrencyStoreService: Decodable {
let e: String
let ok: String
let data: [data]
struct data: Decodable {
let pairs: [pairs]
struct pairs: Decodable {
let pairs: CurrencyStore.Currency
}
}
}
extension CurrencyStore {
init(from CurrencyStoreService: CurrencyStoreService) {
name = "Test"
currencies = []
for data in CurrencyStoreService.data {
for pairs in data.pairs {
currencies.append(pairs.pairs)
}
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let decoder = JSONDecoder()
let serviceStores = try decoder.decode([CurrencyStoreService].self, from: jsonvar)
let stores = serviceStores.map { CurrencyStore (from: $0) }
for store in stores {
for Currency in store.currencies {
print("\(Currency.symbol1) for max \(Currency.maxPrice) or at min \(Currency.minPrice)")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
probably someone has an sipmler way to parse it?
i get the template from here:
i dont implement an requst from the internet to make it simpler...
hopefully some one can help me...
feel free to change the complete code...