反序列化IRestResponse并将其写入控制台C#



我正在使用CalorieInjas API在C#中构建一个简单的控制台应用程序。用户输入成分,API以JSON格式返回各种营养值:
JSON输出
我的问题是,我不知道如何将JSON输出反序列化为数组、列表或字典等格式,也不知道如何向控制台写入列表/字典值。我现在有一个类,它表示输出的JSON数据:

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; }
}

然后是类级别的列表:

public List<Nutrition> NutritionList { get; set; }

我现在拥有的是:

IRestResponse<Nutrition> response = client.Execute<Nutrition>(request);
JsonDeserializer deserial = new JsonDeserializer();

//foreach(var item in NutritionList)
//{
//  Console.WriteLine(item...)
//}
Console.ReadKey();

这可能是一个简单的解决方案,但我就是想不通。我感谢你的帮助:(

简单的解决方案是

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

相关内容

最新更新