قالب وردپرس درنا توس
Home / IOS Development / Has trouble decoding nested dictionary structure from JSON: iOSProgramming

Has trouble decoding nested dictionary structure from JSON: iOSProgramming



I've tried refactoring some code to use Swift 4's Codable for JSON analysis, but none of the examples I see have helped me figure this out.

I'm grabbing JSON data from the Blockchain.info API here: https://blockchain.info/ticker Chapter19659002achte. Here is an example answer:

{"USD": {"15m": 11334.33, "load": 11334.33, "buy": 11334.33, "sell": 11334.33, "symbol": "$"} , "AUD": {"15m": 16697.59, "last": 16697.59, "buy": 16697.59, "sell": 16697.59, "symbol": "$"}, "BRL": {"1

5m": 44679.91, "load": 44679.91, "buy": 44679.91, "sell": 44679.91, "symbol": "R $"}, "CAD": {"15m": 15022.3, "last": 15022.3, "buy": 15022.3 , "sell": 15022.3, "symbol": "$"}, "CHF": {"15m": 11014.13, "last": 11014.13, "buy": 11014.13, "sell": 11014.13, "symbol": " CHF "}," CLP ": {" 15m ": 8048731.03," last ": 8048731.03," buy ": 8048731.03," sell ": 8048731.03," symbol ":" $ "}," CNY ": {" 15m " : 80048.93, "last": 80048.93, "buy": 80048.93, "sell": 80048.93, "symbol": "¥"}, "DKK": {"15m": 75520.61, "last": 75520.61, "buy" : 75520.61, "sell": 75520.61, "symbol": "kr"}, "EUR": {"15m": 10138.66, "load": 10138.66, "buy": 10138.66, "sell": 10138.66, "symbol" : "€"}, "GBP": {"15m": 9415.42, "load": 9415.42, "buy": 9415.42, "sell": 9415 .42, "symbol": "£"}, "HKD": {"15m": 88880.94, "last": 88880.94, "buy": 88880.94, "sell": 88880.94, "symbol": "$"}, "INR": {"15m": 805213.13, "last": 805213.13, "buy": 805213.13, "sell": 805213.13, "symbol": "$"}, "ISK": {"15m": 1391287.41, " load ": 1391287.41," buy ": 1391287.41," sell ": 1391287.41," symbol ":" kr "}," JPY ": {" 15m ": 1196937.71," load ": 1196937.71," buy ": 1196937.71," sell ": 1196937.71," symbol ":" ¥ "}," KRW ": {" 15m ": 1.376461304E7," last ": 1.376461304E7," buy ": 1.376461304E7," sell ": 1.376461304E7," symbol " : "₩"}, "NZD": {"15m": 17518.33, "last": 17518.33, "buy": 17518.33, "sell": 17518.33, "symbol": "$"}, "PLN": {" 15m ": 43759.0," load ": 43759.0," buy ": 43759.0," sell ": 43759.0," symbol ":" zł "}," RUB ": {" 15m ": 739678.06," last ": 739678.06," buy ": 739678.06," sell ": 739678.06," symbol ":" RUB "}," SEK ": {" 15m ": 108189.53," load ": 108189.53," buy ": 108189.53," sell ": 108189.53," symbol ":" kr "}," SGD ": {" 15m ": 15705.03," load ": 15705.03 , "buy": 15705.03, "sell": 15705.03, "symbol": "$"}, "THB": {"15m": 345028.19, "load": 345028.19, "buy": 345028.19, "sell": 345028.19 , "symbol": "฿"}, "TWD": {"15m": 355682.46, "last": 355682.46, "buy": 355682.46, "sell": 355682.46, "symbol": "NT $"}}

How should I structure structures to cope with this? I tried the following, but did not succeed:

struct Ticker: Codable {

easy buy: Double

last: Double [19659002] let sell: Double

let symbol: String

}

struct Currency: Codable { [19659002] la currency: [Ticker]

}

func getJsonData () {

let url = URL : "https://blockchain.info/ticker")!??19199009005????19659002????19459004??URLSession.shared.data Task (with: url) {(data, _, _) in

if la data = data {

la ticker = sample? JSONDecoder (). Decode ([Currency]. Self, from: data)

print (ticker)

}

}. resume ()

}

Any suggestions will be greatly appreciated.


Source link