ios – How can I parse this JSON format when I am having within a property, which is an array of object, two different objects in SWIFT

[ad_1]

the JSON response is as follows:

{
"results": [
{
"vegetarian": true,
"vegan": false,
"glutenFree": true,
"dairyFree": false,
"veryHealthy": false,
"cheap": false,
"veryPopular": false,
"sustainable": false,
"weightWatcherSmartPoints": 9,
"gaps": "no",
"lowFodmap": false,
"aggregateLikes": 9,
"spoonacularScore": 67,
"healthScore": 17,
"creditsText": "Foodista.com – The Cooking Encyclopedia Everyone Can Edit",
"license": "CC BY 3.0",
"sourceName": "Foodista",
"pricePerServing": 195.11,
"id": 656329,
"title": "Pizza bites with pumpkin",
"readyInMinutes": 45,
"servings": 4,
"sourceUrl": "https://www.foodista.com/recipe/SHKG55X4/pizza-bites-with-pumpkin",
"image": "https://spoonacular.com/recipeImages/656329-312x231.jpg",
"imageType": "jpg",
"summary": "Pizza bites with pumpkin might be just the hor d'oeuvre you are searching for. This recipe serves 4. For <b>$1.95 per serving</b>, this recipe <b>covers 22%</b> of your daily requirements of vitamins and minerals. One serving contains <b>309 calories</b>, <b>16g of protein</b>, and <b>22g of fat</b>. Head to the store and pick up garlic, parsley, eggs, and a few other things to make it today. 9 people have tried and liked this recipe. It is brought to you by Foodista. It is an <b>affordable</b> recipe for fans of Mediterranean food. From preparation to the plate, this recipe takes approximately <b>approximately 45 minutes</b>. It is a good option if you're following a <b>gluten free, lacto ovo vegetarian, and primal</b> diet. All things considered, we decided this recipe <b>deserves a spoonacular score of 66%</b>. This score is solid. If you like this recipe, you might also like recipes such as <a href="https://spoonacular.com/recipes/pizza-bites-89274">Pizza Bites</a>, <a href="https://spoonacular.com/recipes/pizza-bites-568070">Pizza Bites</a>, and <a href="https://spoonacular.com/recipes/zucchini-pizza-bites-540333">Zucchini Pizza Bites</a>.",
"cuisines": [
"Mediterranean",
"Italian",
"European"
],
"dishTypes": [
"fingerfood",
"antipasti",
"starter",
"snack",
"appetizer",
"antipasto",
"hor d'oeuvre"
],
"diets": [
"gluten free",
"lacto ovo vegetarian",
"primal"
],
"occasions": [],
"analyzedInstructions": [
{
"name": "",
"steps": [
{
"number": 1,
"step": "Chop pumpkin using a food processor until rice-like.",
"ingredients": [2 items],
"equipment": [1 item]
},
{
"number": 2,
"step": "Saut pumpkin in hot olive oil for 3 minutes. Set aside and let cool.",
"ingredients": [2 items],
"equipment": [],
"length": {2 items}
},
{
"number": 3,
"step": "Mix feta and mozzarella; add, one at a time, eggs.",
"ingredients": [3 items],
"equipment": []
},
{
"number": 4,
"step": "Mix and combine.",
"ingredients": [],
"equipment": []
},
{
"number": 5,
"step": "Add pumpkin and spices, mix well until well blended.",
"ingredients": [2 items],
"equipment": []
},
{
"number": 6,
"step": "Evenly spoon the mixture into the greased muffin tin molds. Press pizza dough down evenly and firmly (the pressing down firmly is very important to make sure they stick together).",
"ingredients": [1 item],
"equipment": [1 item]
},
{
"number": 7,
"step": "Place in the oven and bake for 30 minutes at 200C.",
"ingredients": [],
"equipment": [1 item],
"length": {2 items}
},
{
"number": 8,
"step": "Remove the pizza bites from the oven and let set until cool (this is also very important let the pizza bites set in their pan for 5 10 minutes before removing if you take them out while they are too hot they will break).",
"ingredients": [1 item],
"equipment": [2 items],
"length": {2 items}
}
]
}
],
"spoonacularSourceUrl": "https://spoonacular.com/pizza-bites-with-pumpkin-656329"
},
{
"vegetarian": false,
"vegan": false,
"glutenFree": true,
"dairyFree": false,
"veryHealthy": false,
"cheap": false,
"veryPopular": false,
"sustainable": false,
"weightWatcherSmartPoints": 18,
"gaps": "no",
"lowFodmap": false,
"aggregateLikes": 5,
"spoonacularScore": 65,
"healthScore": 18,
"creditsText": "coffeebean",
"license": "spoonacular's terms",
"sourceName": "spoonacular",
"pricePerServing": 238.84,
"id": 622598,
"title": "Pittata - Pizza Frittata",
"author": "coffeebean",
"readyInMinutes": 30,
"servings": 2,
"sourceUrl": "https://spoonacular.com/-1418233867224",
"image": "https://spoonacular.com/recipeImages/622598-312x231.jpg",
"imageType": "jpg",
"summary": "The recipe Pittata - Pizza Frittatan is ready <b>in roughly 30 minutes</b> and is definitely a great <b>gluten free and primal</b> option for lovers of Mediterranean food. This recipe makes 2 servings with <b>587 calories</b>, <b>44g of protein</b>, and <b>42g of fat</b> each. For <b>$2.39 per serving</b>, this recipe <b>covers 29%</b> of your daily requirements of vitamins and minerals. A mixture of parmesan cheese, mozzarella cheese, milk, and a handful of other ingredients are all it takes to make this recipe so delicious. To use up the eggs you could follow this main course with the <a href="https://spoonacular.com/recipes/rose-levy-beranbaums-chocolate-tomato-cake-with-mystery-ganache-27408">Rose Levy Beranbaum's Chocolate Tomato Cake with Mystery Ganache</a> as a dessert. Try <a href="https://spoonacular.com/recipes/pizza-frittata-500787">Pizza Frittata</a>, <a href="https://spoonacular.com/recipes/pizza-frittata-159328">Pizza Frittata</a>, and <a href="https://spoonacular.com/recipes/pizza-frittata-123747">Pizza Frittata</a> for similar recipes.",
"cuisines": [
"Mediterranean",
"Italian",
"European"
],
"dishTypes": [
"lunch",
"main course",
"main dish",
"dinner"
],
"diets": [
"gluten free",
"primal"
],
"occasions": [],
"analyzedInstructions": [
{
"name": "",
"steps": [
{
"number": 1,
"step": "Pre-heat oven to ~450°F (230°C), top heat only.",
"ingredients": [],
"equipment": [
{
"id": 404784,
"name": "oven",
"localizedName": "oven",
"image": "oven.jpg",
"temperature": {
"number": 450,
"unit": "Fahrenheit"
}
}
]
},
{
"number": 2,
"step": "On the stove, heat cooking fat in a large oven-safe skillet over medium heat.",
"ingredients": [
{
"id": 0,
"name": "cooking fat",
"localizedName": "cooking fat",
"image": ""
}
],
"equipment": [
{
"id": 404645,
"name": "frying pan",
"localizedName": "frying pan",
"image": "pan.png"
},
{
"id": 404794,
"name": "stove",
"localizedName": "stove",
"image": "oven.jpg"
},
{
"id": 404784,
"name": "oven",
"localizedName": "oven",
"image": "oven.jpg"
}
]
},
{
"number": 3,
"step": "Beat the eggs with the milk (again milk is optional), salt, pepper, and any herbs or spices desired.",
"ingredients": [
{
"id": 1002030,
"name": "pepper",
"localizedName": "pepper",
"image": "pepper.jpg"
},
{
"id": 2035,
"name": "spices",
"localizedName": "spices",
"image": "spices.png"
},
{
"id": 1002044,
"name": "herbs",
"localizedName": "herbs",
"image": "mixed-fresh-herbs.jpg"
},
{
"id": 1123,
"name": "egg",
"localizedName": "egg",
"image": "egg.png"
},
{
"id": 1077,
"name": "milk",
"localizedName": "milk",
"image": "milk.png"
},
{
"id": 2047,
"name": "salt",
"localizedName": "salt",
"image": "salt.jpg"
}
],
"equipment": []
},
{
"number": 4,
"step": "Add any desired add-ins (other than cheese) to the skillet. For the pittata, we didn't use anything, but this is when you would add your cooked vegetables or raw vegetables that don't require much cooking (e.g. bell pepper).",
"ingredients": [
{
"id": 10211821,
"name": "bell pepper",
"localizedName": "bell pepper",
"image": "bell-pepper-orange.png"
},
{
"id": 11583,
"name": "vegetable",
"localizedName": "vegetable",
"image": "mixed-vegetables.png"
},
{
"id": 1041009,
"name": "cheese",
"localizedName": "cheese",
"image": "cheddar-cheese.png"
}
],
"equipment": [
{
"id": 404645,
"name": "frying pan",
"localizedName": "frying pan",
"image": "pan.png"
}
]
},
{
"number": 5,
"step": "Pour in the egg mixture and tilt the skillet to ensure it evenly surrounds any add-ins. Now distribute the shredded cheese and pepperoni slices (if using).",
"ingredients": [
{
"id": 1007057,
"name": "pepperoni slices",
"localizedName": "pepperoni slices",
"image": "pepperoni.png"
},
{
"id": 1011026,
"name": "shredded cheese",
"localizedName": "shredded cheese",
"image": "cheddar-cheese.png"
},
{
"id": 1123,
"name": "egg",
"localizedName": "egg",
"image": "egg.png"
}
],
"equipment": [
{
"id": 404645,
"name": "frying pan",
"localizedName": "frying pan",
"image": "pan.png"
}
]
},
{
"number": 6,
"step": "Allow the eggs to cook on the stove until the edges of the eggs are set. The center will still be a bit jiggly. Now is a good time to top with Parmesan cheese (be generous!)",
"ingredients": [
{
"id": 1033,
"name": "parmesan",
"localizedName": "parmesan",
"image": "parmesan.jpg"
},
{
"id": 1123,
"name": "egg",
"localizedName": "egg",
"image": "egg.png"
}
],
"equipment": [
{
"id": 404794,
"name": "stove",
"localizedName": "stove",
"image": "oven.jpg"
}
]
},
{
"number": 7,
"step": "Transfer the pan to the hot oven and bake until the eggs are puffed and browned with a firm center.",
"ingredients": [
{
"id": 1123,
"name": "egg",
"localizedName": "egg",
"image": "egg.png"
}
],
"equipment": [
{
"id": 404784,
"name": "oven",
"localizedName": "oven",
"image": "oven.jpg"
},
{
"id": 404645,
"name": "frying pan",
"localizedName": "frying pan",
"image": "pan.png"
}
]
},
{
"number": 8,
"step": "Remove from oven and let cool for a couple minutes before cutting into wedges.",
"ingredients": [],
"equipment": [
{
"id": 404784,
"name": "oven",
"localizedName": "oven",
"image": "oven.jpg"
}
]
}
]
}
],
"spoonacularSourceUrl": "https://spoonacular.com/pittata-pizza-frittata-622598"
},
"offset": 0,
"number": 10,
"totalResults": 37
}

The problem here is how to format it to an object when I have one of the objects in the array, in the “results” attribute, having an additional attribute called “author” and the other objects do not have this attribute. The same thing in the “steps” attribute you will find an attribute called “length” which does not occur to all the objects inside. I tried to handle it as follows but it could not parse it well.

struct DetailedRecipeFormat:Codable{
    let results:[ResultFood]
    let offset:Int
    let number:Int
    let totalResults:Int
}


struct ResultFood:Codable{
    let vegetarian:Bool
    let vegan:Bool
    let glutenFree:Bool
    let dairyFree:Bool
    let veryHealthy:Bool
    let cheap:Bool
    let veryPopular:Bool
    let sustainable:Bool
    let weightWatcherSmartPoints:Int
    let gaps:String
    let lowFodmap:Bool
    let aggregateLikes:Int
    let spoonacularScore:Int
    let healthScore:Int
    let creditsText:String
    let license:String
    let sourceName:String
    let pricePerServing:Int
    let id:Int
    let title:String
    let author:String
    let readyInMinutes:Int
    let servings:Int
    let sourceUrl:String
    let image:String
    let imageType:String
    let summary:String
    let cuisines:[String]
    let dishTypes:[String]
    let diets:[String]
    let occasions:[String]
    let analyzedInstructions:[Instruction]
    let spoonacularSourceUrl:String
}

struct Instruction:Codable{
    let name:String
    let steps:[Step]
}


struct Step:Codable{
    let number:Int
    let step:String
    let ingredients:[Ingredient]
    let equipment:[Equipment]
    let length:[Length]
}

struct Ingredient:Codable{
    let id:Int
    let name:String
    let localizedName:String
    let image:String
}

typealias Equipment = Ingredient

struct Length:Codable{
    let number:Int
    let unit:String
}


[ad_2]

Source link

Leave a Reply

Your email address will not be published.