[ad_1]
I am currently using an API to query some information and am getting back a JSON string. I’ve looked around but am unable to get the string into a dictionary. Thoughts?
let url = URL(string: "https://api.spoonacular.com/food/products/upc/041631000564?apiKey=xxx")!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
let str = String(data: data, encoding: .utf8)!
print(str)
}
task.resume()
The string printed:
{"id":30004,"title":"Swan Flour","price":0.0,"likes":0,"badges":["egg_free","wheat_free","grain_free","peanut_free","primal","vegetarian","nut_free","vegan","pescetarian","dairy_free","paleo","gluten_free"],"importantBadges":["gluten_free","paleo","primal","wheat_free","grain_free"],"nutrition":{"nutrients":[{"name":"Calcium","amount":0.0,"unit":"mg","percentOfDailyNeeds":0.0},{"name":"Carbohydrates","amount":8.0,"unit":"g","percentOfDailyNeeds":2.67},{"name":"Calories","amount":30.0,"unit":"kcal","percentOfDailyNeeds":1.5},{"name":"Fat","amount":0.0,"unit":"g","percentOfDailyNeeds":0.0},{"name":"Saturated Fat","amount":0.0,"unit":"g","percentOfDailyNeeds":0.0},{"name":"Trans Fat","amount":0.0,"unit":"g","percentOfDailyNeeds":0.0},{"name":"Iron","amount":0.0,"unit":"mg","percentOfDailyNeeds":0.0},{"name":"Protein","amount":0.0,"unit":"g","percentOfDailyNeeds":0.0},{"name":"Sodium","amount":0.0,"unit":"mg","percentOfDailyNeeds":0.0},{"name":"Vitamin A","amount":0.0,"unit":"IU","percentOfDailyNeeds":0.0},{"name":"Vitamin C","amount":0.0,"unit":"mg","percentOfDailyNeeds":0.0},{"name":"Net Carbohydrates","amount":8.0,"unit":"g","percentOfDailyNeeds":2.91}],"caloricBreakdown":{"percentProtein":0.0,"percentFat":0.0,"percentCarbs":100.0},"calories":30.0,"fat":"0g","protein":"0g","carbs":"8g"},"servings":{"number":34.0,"size":1.0,"unit":"tbsp"},"spoonacularScore":99.0,"breadcrumbs":[],"aisle":null,"description":null,"image":"https://spoonacular.com/productImages/30004-312x231.jpg","imageType":"jpg","images":["https://spoonacular.com/productImages/30004-90x90.jpg","https://spoonacular.com/productImages/30004-312x231.jpg","https://spoonacular.com/productImages/30004-636x393.jpg"],"generatedText":"Swan Flour: This product is an awesome fit if you are searching for a healthy flour. This product has 1 ingredient (in our experience: the fewer ingredients, the better!) This product contains no ingredients that some research suggests you should avoid. One serving of this product provides 30 calories, 0g grams of fat, 0g grams of protein, and 8g grams of carbs.","upc":"041631000564","brand":null,"ingredients":[{"name":"starch","safety_level":null,"description":null},{"name":"potato starch","safety_level":null,"description":null}],"ingredientCount":2,"ingredientList":"Potato Starch"}
[ad_2]