I'm using CalorieNinjas API to build a simple console app in C#.
User enters an ingredient and the API returns various nutrition values in JSON format:
JSON output
The problem on my side is, that I don't know exactly how to deserialize the JSON output to some format like array, List or Dictionary and how to write the list/dictionary values to console.
What I have right now is a class which represents the outputed JSON data:
public class Nutrition
{
public double sugar_g { get; set; }
public double fiber_g { get; set; }
public int serving_size_g { get; set; }
public int sodium_mg { get; set; }
public string name { get; set; }
public int potassium_mg { get; set; }
public int fat_saturated_g { get; set; }
public double fat_total_g { get; set; }
public double calories { get; set; }
public int cholesterol_mg { get; set; }
public double protein_g { get; set; }
public double carbohydrates_total_g { get; set; }
}
and then a List on class level:
public List<Nutrition> NutritionList { get; set; }
What I have right now is:
IRestResponse<Nutrition> response = client.Execute<Nutrition>(request);
JsonDeserializer deserial = new JsonDeserializer();
//foreach(var item in NutritionList)
//{
// Console.WriteLine(item...)
//}
Console.ReadKey();
It's probably a simple solution, but I just can't figure it out. I am thankful for any help :)