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

时间:2014-06-22 11:11:36

标签: c# json windows-phone

这是我的json,我需要访问出勤数组中每个对象下的值:

{"name":" ","course":"","attendance":[{"name":"INTERNATIONAL FINANCE","type":"Theory","conducted":"55","present":"50"},{"name":"INDIAN CONSTITUTION","type":"Theory","conducted":"6","present":"6"}]}

这是我的代码:

public class Att
{

    public class Attendance
    {
        public string name { get; set; }
        public string type { get; set; }
        public string conducted { get; set; }
        public string present { get; set; }
    }


    public Att(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jUser = jObject;

        name = (string)jUser["name"];
        course = (string)jUser["course"];
        attender = jUser["attendance"].ToList<Attendance>;

    }

    public string name { get; set; }
    public string course { get; set; }
    public string email { get; set; }
    //public Array attend { get; set; }

    public List<Attendance> attender { get; set; }
}

我遇到问题的是attender = jUser["attendance"].ToList<Attendance>;行。它说,

  

无法将方法组ToList转换为非委托类型。你打算调用这个方法吗?

如何访问这些值?

2 个答案:

答案 0 :(得分:22)

你有一个错字!

  

出席率 vs 关注度

这应该有效

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

您可以在DotNetFiddle

找到正在运行的结果

答案 1 :(得分:3)

你想写:

attender = jUser["attendence"].ToList<Attendance>(); // notice the ()

关于错误: 当你不把那些parantheses放在那里时,C#假设你想要 将函数(委托)ToList分配给变量attender,而不是 调用它并分配其返回值。