0

I have server with API responses that looks like:

{
  "result": true
}

{
  "result": {"aaa":"bbb"}
}

{
  "result":[{"aaa":"bbb"},{"aaa":"bbb"}]
}

Now I use JSONDecoder with structs like this:

struct MyObject: Codable {
    var aaa: String
}

struct ResultTrue: Codable {
    var result: Bool
}

struct ResultMyObjectDict: Codable {
    var result: MyObject
}

struct ResultMyObjectArray: Codable {
    var result: [MyObject]
}

But it looks overwhelmed - I use ResultTrue.self, ResultMyObjectDict.self, ResultMyObjectArray.self and always check .result...

Can I tell to JSON decoder, that my data is always located at key result and use something like simple Bool.self, MyObject.self, [MyObject].self with struct MyObject: Codable { var aaa: String } ?

Stanislav Chernischuk
  • 975
  • 1
  • 11
  • 24

0 Answers0