I come across the problem "Expected to decode Array but found a dictionary instead.", underlyingError: nil" when I try to decode a jason file. The following is my decode process!
Can someone give me some advice? It's urgent! Thanks!
I come across the problem "Expected to decode Array but found a dictionary instead.", underlyingError: nil" when I try to decode a jason file. The following is my decode process!
Can someone give me some advice? It's urgent! Thanks!
What does JSON file look like?
From the URL in the screenshot:
{
"data": {
"url": "https://www.92degreescoffee.com/",
"photo_url": null,
"phone_number": null,
"opening_hours": {
"monday": "07:45 - 19:00",
"tuesday": "07:45 - 19:00",
"wednesday": "07:45 - 19:00",
"thursday": "07:45 - 19:00",
"friday": "07:45 - 19:00"
}
},
"code": 1
}
Yeah, thanks! I'm new guy here and only 1 pic is allowed:sweat_smile:
Ok, the type doesn't match the data.
try
struct OpenHour: Decodable {
var monday, tuesday, wednesday, thursday, friday: String?
}
struct ShopSpecific: Decodable {
var url, photoUrl, phoneNumber: String?
var openingHours: OpenHour
}
struct SpecificInfo: Decodable {
var data: ShopSpecific, code: Int?
}
And you can do
decoder.keyDecodingStrategy = .convertFromSnakeCase
before decoding to eliminate snake naming.
Specifically, the problem is that you have declared this:
var opening_hours: [openHour]
but the data doesn't have an array of openHour
; it just has a single instance. You want this:
var opening_hours: openHour
Same things applies to \SpecificInfo.data
as well.
PS
you can put code in ```
```
like this
```
And it'll look
like this
It works! Thank you so much!
Yeah I get it and it works. Thank you so much!