如何对网络调用进行编码,以便我只返回非 nil 或空白的字段



我需要下载一个食谱的成分和测量值。不同的食谱有不同的配料量和尺寸。recipe API最多提供20个配料对象。不同的食谱有不同数量的配料。我不想显示空数据或null数据。我该如何对其进行编码以仅获取非空字符串或null的成分呢?

希望我解释得很好。如果我需要补充更多的信息,请告诉我。我在下面添加了JSON。

"meals": [
{
"idMeal": "52772",
"strMeal": "Teriyaki Chicken Casserole",
"strDrinkAlternate": null,
"strCategory": "Chicken",
"strArea": "Japanese",
"strInstructions": "Preheat oven to 350° F. Spray a 9x13-inch baking pan with non-stick spray.rnCombine soy sauce, ½ cup water, brown sugar, ginger and garlic in a small saucepan and cover. Bring to a boil over medium heat. Remove lid and cook for one minute once boiling.rnMeanwhile, stir together the corn starch and 2 tablespoons of water in a separate dish until smooth. Once sauce is boiling, add mixture to the saucepan and stir to combine. Cook until the sauce starts to thicken then remove from heat.rnPlace the chicken breasts in the prepared pan. Pour one cup of the sauce over top of chicken. Place chicken in oven and bake 35 minutes or until cooked through. Remove from oven and shred chicken in the dish using two forks.rn*Meanwhile, steam or cook the vegetables according to package directions.rnAdd the cooked vegetables and rice to the casserole dish with the chicken. Add most of the remaining sauce, reserving a bit to drizzle over the top when serving. Gently toss everything together in the casserole dish until combined. Return to oven and cook 15 minutes. Remove from oven and let stand 5 minutes before serving. Drizzle each serving with remaining sauce. Enjoy!",
"strMealThumb": "https://www.themealdb.com/images/media/meals/wvpsxx1468256321.jpg",
"strTags": "Meat,Casserole",
"strYoutube": "https://www.youtube.com/watch?v=4aZr5hZXP_s",
"strIngredient1": "soy sauce",
"strIngredient2": "water",
"strIngredient3": "brown sugar",
"strIngredient4": "ground ginger",
"strIngredient5": "minced garlic",
"strIngredient6": "cornstarch",
"strIngredient7": "chicken breasts",
"strIngredient8": "stir-fry vegetables",
"strIngredient9": "brown rice",
"strIngredient10": "",
"strIngredient11": "",
"strIngredient12": "",
"strIngredient13": "",
"strIngredient14": "",
"strIngredient15": "",
"strIngredient16": null,
"strIngredient17": null,
"strIngredient18": null,
"strIngredient19": null,
"strIngredient20": null,
"strMeasure1": "3/4 cup",
"strMeasure2": "1/2 cup",
"strMeasure3": "1/4 cup",
"strMeasure4": "1/2 teaspoon",
"strMeasure5": "1/2 teaspoon",
"strMeasure6": "4 Tablespoons",
"strMeasure7": "2",
"strMeasure8": "1 (12 oz.)",
"strMeasure9": "3 cups",
"strMeasure10": "",
"strMeasure11": "",
"strMeasure12": "",
"strMeasure13": "",
"strMeasure14": "",
"strMeasure15": "",
"strMeasure16": null,
"strMeasure17": null,
"strMeasure18": null,
"strMeasure19": null,
"strMeasure20": null,
"strSource": null,
"strImageSource": null,
"strCreativeCommonsConfirmed": null,
"dateModified": null
}

)}

在您的情况下,通过网络调用是不可能的。你需要做一个网络调用来获取Data对象,然后反序列化它。您应该提前知道JSON包含的所有数据块。所以你需要创建模型对象。对于要解析的结构,所有可以为空的字段都应该标记为可选值。例如let strMeasure16: String?:您还可以根据field的每个值构建自己的逻辑:

guard let measure = strMeasure16, !measure.isEmpty else {
print("Value is empty or nil")
return
}
// do what you need to do with this value

如果可能的话,如果数据提供者可以在数组中构造和发送所需的值,那将容易得多。如果没有这种可能性,或者您使用开放API,只有这样做。我希望它对你有所帮助。

最新更新