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))