I have this data
{
"data":{
"date":"07-03-2022",
"type":"airplane"
}
}
and my code is
struct JsonResult: View{
enum LoadingState {
case idle, loading, loaded(UserData), failure(Error)
}
@State private var state : LoadingState = .idle
var body: some View {
VStack {
switch state {
case .idle: EmptyView()
case .loading: ProgressView()
case .loaded(let userData):
VStack(alignment: .leading) {
Text(userData.date)
.font(.headline)
Text(userData.type)
.font(.headline)
}
case .failure(let error): Text(error.localizedDescription)
}
}.task {
await loadData()
}
}
struct Response: Encodable, Decodable {
var data: UserData
}
struct UserData: Codable {
var date: String
var type: String
private enum CodingKeys: String, CodingKey {
case date = "date"
case type = "type"
}
}
func loadData() async {
state = .loading
guard let url = URL(string: "MyUrl(related to cloud functions") else {
state = .failure(URLError(.badURL))
return
}
do {
let (data,_) = try await URLSession.shared.data(from: url)
// more code
let decodedResponse = try JSONDecoder().decode(Response.self, from: data)
state = .loaded(decodedResponse.data)
} catch {
state = .failure(error)
print(error) // this shows the real DecodingError
}
}
}
and after all this work, i want to get data from cloud functions, I got the same error, "The given data was not valid JSON.", although the data structure is valid json but It says data was not valid json ! any solution ? Thank you alot