c# – 如何将JSON数组转换为List <>?

前端之家收集整理的这篇文章主要介绍了c# – 如何将JSON数组转换为List <>?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的json,我需要访问出勤数组中每个对象下的值:
  1. {"name":" ","course":"","attendance":[{"name":"INTERNATIONAL FINANCE","type":"Theory","conducted":"55","present":"50"},{"name":"INDIAN CONSTITUTION","conducted":"6","present":"6"}]}

这是我的代码

  1. public class Att
  2. {
  3.  
  4. public class Attendance
  5. {
  6. public string name { get; set; }
  7. public string type { get; set; }
  8. public string conducted { get; set; }
  9. public string present { get; set; }
  10. }
  11.  
  12.  
  13. public Att(string json)
  14. {
  15. JObject jObject = JObject.Parse(json);
  16. JToken jUser = jObject;
  17.  
  18. name = (string)jUser["name"];
  19. course = (string)jUser["course"];
  20. attender = jUser["attendance"].ToList<Attendance>;
  21.  
  22. }
  23.  
  24. public string name { get; set; }
  25. public string course { get; set; }
  26. public string email { get; set; }
  27. //public Array attend { get; set; }
  28.  
  29. public List<Attendance> attender { get; set; }
  30. }

它是出席者= jUser [“出席率”] .ToList<出席率&gt ;;我遇到问题的一句话.它说,

Cannot convert method group ToList to non-delegate type. Did you intend to invoke this method?

我如何访问这些值?

解决方法

你有一个错字!

attendance vs attendence

这应该工作

  1. attender = jUser["attendance"].ToObject<List<Attendance>>();

您可以在DotNetFiddle找到运行结果

猜你在找的C#相关文章