-3

I am getting json response from the server and after that i will decode this json data into my model class. But it shows error "The data couldn’t be read because it isn’t in the correct format". I am using this feature first time in iOS. I actually don't know how to resolve this error. I already add model class code and getting data code below. Please check.

Model Class

struct PlacesResult: Decodable {
    let html_attributions: [String]?
    let next_page_token: String?
    let results : [Result]?
}

struct Result: Decodable {
    let formatted_address: String?
    let geometry: Geometry?
    let icon: String?
    let id: Int?
    let name: String?
    var photos: [Photos]?
    let place_id: String?
    let rating: String?
    let types: [String]?
    let user_ratings_total: Int?
}

struct Geometry: Decodable {
    let location: Location?
    let viewport: ViewPort?
}

struct Location: Decodable {
    let lat: String?
    let lng: String?
}

struct ViewPort: Decodable {
    let northeast: NorthEast?
    let southwest: SouthWest?
}

struct NorthEast: Decodable {
    let lat: String?
    let lng: String?
}

struct SouthWest: Decodable {
    let lat: String?
    let lng: String?
}

struct Photos: Decodable {
    var height: Int?
    var width: Int?
    var photo_reference: String?
    var html_attributions: [String]?
}

Decode Json Data

if let data = responseObject.dataObject as? Data {

do {
    let placesData = try JSONDecoder().decode(PlacesResult.self, from: data)
    print(placesData.next_page_token)
    //print(placesData.results?.count)
   }
catch {
        print("Error : \(error.localizedDescription)")
      }
}

Error:

Error : typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "results", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "geometry", intValue: nil), CodingKeys(stringValue: "location", intValue: nil), CodingKeys(stringValue: "lat", intValue: nil)], debugDescription: "Expected to decode String but found a number instead.", underlyingError: nil))

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    You also need to show (in your question, as text) a relevant snapshot of the actual JSON you are trying to parse. – rmaddy May 01 '19 at 06:07
  • And only make a struct property optional if the JSON may not have that value. – rmaddy May 01 '19 at 06:09
  • Please give your JSON response. – Ankur Lahiry May 01 '19 at 06:14
  • 1
    **Never ever** print `error.localizedDescription` in a `JSONDecoder` catch block because you get this generic and pretty meaningless error message. Print always only the `error` instance (`print("Error:", error)`). It tells you exactly what's wrong and even where. – vadian May 01 '19 at 07:26

1 Answers1

1

The added DecodingError message is very comprehensive. It contains the type of the error, the key path and the description.

To clarify the structure this is the message split in multiple lines

Error : typeMismatch(Swift.String, Swift.DecodingError.Context(
        codingPath:   
            [CodingKeys(stringValue: "results", intValue: nil), 
               _JSONKey(stringValue: "Index 0", intValue: 0),
             CodingKeys(stringValue: "geometry", intValue: nil),
             CodingKeys(stringValue: "location", intValue: nil), 
             CodingKeys(stringValue: "lat", intValue: nil)], 
         debugDescription: 
             "Expected to decode String but found a number instead.", underlyingError: nil)) 
  • The error type at the beginning is a type mismatch.
  • The codingPath describes the key path. It points to key lat in dictionary for key location in dictionary for key geometry in the first item of the array for key results (aka results[0].geometry.location.lat)
  • The debugDescription describes the error. Expected is the proposed wrong type, found is the actual type.
    So the value of lat is not a string, it's a number, most likely Double

All JSON keys and string values are wrapped in double quotes so the String type can be recognized immediately.

vadian
  • 274,689
  • 30
  • 353
  • 361