Reputation: 228
{
"uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_f9e656dd9d2b4db9816340687d01722c",
"calories" : 38,
"totalWeight" : 113.25,
"dietLabels" : [ "LOW_FAT" ],
"healthLabels" : [ "SUGAR_CONSCIOUS", "VEGAN", "VEGETARIAN", "PEANUT_FREE", "TREE_NUT_FREE", "ALCOHOL_FREE" ],
"cautions" : [ ],
"totalNutrients" : {
"ENERC_KCAL" : {
"label" : "Energy",
"quantity" : 38.505,
"unit" : "kcal"
},
"FAT" : {
"label" : "Fat",
"quantity" : 0.41902500000000004,
"unit" : "g"
},
"FASAT" : {
"label" : "Saturated",
"quantity" : 0.044167500000000005,
"unit" : "g"
},
"FAMS" : {
"label" : "Monounsaturated",
"quantity" : 0.0124575,
"unit" : "g"
},
"FAPU" : {
"label" : "Polyunsaturated",
"quantity" : 0.043035000000000004,
"unit" : "g"
}
}
}
/* * networking method */
func getNutritionData(url: String) {
Alamofire.request(url, method: .get)
.responseString { response in
if response.result.isSuccess {
print("Sucess! Got the Nutrition data")
let nutritionJSON : JSON = JSON(response.result.value!)
//print(nutritionJSON)
self.updateNutritionData(json: nutritionJSON)
} else {
print("Error: \(String(describing: response.result.error))")
self.caloriesLable.text = "Connection Issues"
}
}
}
func updateNutritionData(json: JSON) {
let calories = json["calories"].intValue
print(calories)
}
^ When I try to get the calories for example, I get nil
In the nutritionData method I tried using .responseJSON but it was throwing an error so I switched to .responseString. I want to get the "totalNutrients" information from that JSON. Help would be appreciated
Upvotes: 0
Views: 5619
Reputation: 515
You should definitely be using responseJSON()
if you just wanna serialize JSON. If you're trying to use it with a Decodable
object, you should be using responseData()
and decoding one of your custom types with the data. Always validate.
func getNutritionData(url: String) {
Alamofire.request(url)
.validate(statusCode: 200...200)
.validate(contentType: ["application/json"])
.responseJSON { (response) in
switch response.result {
case .success(let json):
// Do something with the json.
let dict = json as? [String: Any]
print(dict["<KEY>"]!)
case .failure(let error):
print(error)
}
}
}
Upvotes: 0
Reputation: 2120
Ary alamofire.response, and then directly parse data in to JSON
Alamofire.request(url, method: .get).response { response in
if response.result.isSuccess {
let nutritionJSON : JSON = JSON(response.data)
self.updateNutritionData(json: nutritionJSON)
} else {
print("Error: \(String(describing: response.result.error))")
self.caloriesLable.text = "Connection Issues"
}
}
func updateNutritionData(json: JSON) {
let calories = json["calories"].intValue
print(calories)
}
Upvotes: 0
Reputation: 100541
It's better to use method with responseJSON
instead of responseString
Alamofire.request(url, method: .get, parameters:nil, encoding: JSONEncoding.default).responseJSON { response in
print(response)
if let json = response.result.value as? [String:Any] {
print(json["calories"])
}
}
and then try
Upvotes: 1