首页 > 解决方案 > 如何编码网络调用,以便我只返回不为零或空白的字段

问题描述

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

希望我解释得很好。如果我需要添加更多信息,请告诉我。我在下面添加了 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.\r\nCombine 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.\r\nMeanwhile, 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.\r\nPlace 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.\r\n*Meanwhile, steam or cook the vegetables according to package directions.\r\nAdd 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
}

] }

标签: jsonswiftapinetworking

解决方案


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

guard let measure = strMeasure16, !measure.isEmpty else {
    print("Value is empty or nil")
    return
}

// do what you need to do with this value

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


推荐阅读