3

I have DataModel data type:

class DataModel: Decodable {
    var data : Any?
    var name : String?
}

server json:

{
   "data":"success",
   "name":"john"
}

or

{
    "data": {
        "result": {
            "shopcode": 0,
            "shopname": ""
        }
    },
    "name": "john"
}

the data property I not sure type, sometime is [String: Any] or String or null.
I search some answer, but it not I want, so please don't set question [duplicate]
How to decode a property with type of any arbitrary JSON dictionary in Swift 4 decodable protocol
Any when decoding JSON with Codable?

How to solve this? Have any way let data property decoder to [String: Any], String, nil?

English is not my native language; please excuse typing errors

pacification
  • 5,838
  • 4
  • 29
  • 51
Karim
  • 322
  • 4
  • 20
  • `Any` cannot be used in `Codable` protocol because it requires type information beforehand. [check answer](https://stackoverflow.com/a/48297432/2857130) – staticVoidMan Apr 04 '18 at 06:36
  • Share with us what `data` looks like in both cases: as a `String` and as `[String:Any]` – staticVoidMan Apr 04 '18 at 06:39
  • `Codable` relies on concrete types. If the JSON contains different types for a key you have to write a custom initializer which handles the cases. – vadian Apr 04 '18 at 06:40
  • @vadian yes,I think so.I maybe can write extension or orrwrite Decodable solve this – Karim Apr 04 '18 at 06:54

0 Answers0