2

I get a response from the server of this type, where the name of the pair is the key:

{
    "BTC_BCN":{
        "last":"0.00000057",
        "percentChange":"0.03636363",
        "baseVolume":"47.08463318"
    },
    "BTC_BELA":{
        "last":"0.00001281",
        "percentChange":"0.07376362",
        "baseVolume":"5.46595029"
    }
}

How can I decode it to a structure where the property name is the key from json:

struct Pair: Codable {
    let name: String // need to put the name of the pair ("BTC_BCN")
    let last: String
    let percentChange: String
    let baseVolume: String
}
vbb
  • 61
  • 3
  • Did you try implementing custom encoder and decoder? This might help you https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types – Kushal Jogi Apr 22 '18 at 23:25
  • For an almost exact parallel, see my answer here: https://stackoverflow.com/a/49967696/341994. Instead of making a "flat" single struct, I turn the unknown keys into dictionary keys and put the deeper struct as the value, so it is hierarchical just like your JSON. But that's a minor difference. – matt Apr 22 '18 at 23:51
  • That's what I finally came up with: https://stackoverflow.com/a/50186681/9682968 – vbb May 05 '18 at 07:00

0 Answers0