【发布时间】:2021-06-28 16:19:31
【问题描述】:
我正在使用 CalorieNinjas API 在 C# 中构建一个简单的控制台应用程序。
用户输入一种成分,API 以 JSON 格式返回各种营养值:
JSON output
我这边的问题是,我不确切知道如何将 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();
这可能是一个简单的解决方案,但我就是想不通。感谢您的帮助:)
【问题讨论】:
标签: c# json api restsharp json-deserialization