In the future, please supply a github link or post your code directly in the forums. That makes it so much easier for other people to assist you.
- Your JSON is invalid. You cannot have an array inside an object without a key, which is what you have here:
{
[
{
"id":1001,
"name":"Chapathi",
"imageName":"Chapathi",
"description":"Chapathi is a type of roti bread that is served all around the world.It is made of of wheat flour, oil, and water. Rolled and placed on a tawa.",
"category":"Tiffin"
},
That first {
starts an object, then it's followed by a [
, which starts an array. But you need the array labeled with some kind of key. That's what the error No string key for value in object around character 3.
means.
But let's say we just add a key "restaurants":
to make that validate.
{
"restaurants": [
{
"id":1001,
"name":"Chapathi",
"imageName":"Chapathi",
"description":"Chapathi is a type of roti bread that is served all around the world.It is made of of wheat flour, oil, and water. Rolled and placed on a tawa.",
"category":"Tiffin"
},
- Your JSON is still invalid:
{
"id": 1014,
"name": "Egg Kurma"
"imageName": "eggkurma",
"description": "Slit eggs boiled and mixed with gravy. Made up of spices and coconut. Great for both rice and chapathi.",
"category": "Curries"
},
You need a comma after "name": "Egg Kurma"
.
So fix those two issues and then your JSON will validate.
- You need a struct into which to decode the
restaurants
array from the JSON.
struct Restaurants: Decodable {
let restaurants: [Rest]
}
struct Rest: Hashable, Codable, Identifiable{
var id: Int
var name: String
var category: String
var description: String
var imageName: String
enum Category: String, CaseIterable{
case Tiffin = "Tiffin"
case Rice = "Rice"
case Appetizers = "Appetizers"
case Curries = "Curries"
}
}
let restaurants: Restaurants = load("Rest.json")
And that works.