1

I am using JSONDecoder to convert JSON into Objects.

Here is the code. It usually works fine.

         do{
                print("Hi")
                       if Data != nil{
                        let model =  try JSONDecoder().decode(Base.self, from: Data!)
                       }
            }
            catch{
                print("Error:\(error)")
            }

But I am getting below Error

Error:typeMismatch(Swift.Double, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "rod", intValue: nil), CodingKeys(stringValue: "nod", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "meta", intValue: nil), CodingKeys(stringValue: "label", intValue: nil)], debugDescription: "Expected to decode Double but found a string/data instead.", underlyingError: nil))

It used to work fine. Any help in resolving this would be appreciated

Naveen
  • 77
  • 6
  • 2
    Please **read** the error message carefully. It's pretty clear. The value for key `label` in the dictionary `meta` in the first item of the array `nod` in the dictionary `rod` is a `String` rather than expected `Double`. – vadian Mar 28 '20 at 16:17

1 Answers1

1

The issue seems to be a data type issue.

You have to investigate the Decodable protocol in JSONDecoder().decode which is in your case

Base.self

Basically one of the meta class/struct member is Double while it's supposed to be String.

I'm not doing any advertisement but personally I find https://app.quicktype.io/ easy to use and it's mostly error free when it comes to generating Decodable protocol for any JSON.

I usually copy paste my JSON in QuickType and it'll generate me the Structs to hold my JSON value and I copy paste these Structs to my code

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241