0

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 :)

awertol
  • 5
  • 3
  • Try this: https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net – Scott Hannen Apr 01 '21 at 17:40

1 Answers1

0

The simple solution is

List<Nutrition> nutritionList= client.Execute<List<Nutrition>>(request).Data;