Search code examples
c#jsonwindows-phone

How to convert JSON Array to List<>?


This is my json and I need to access the values under each object in the attendance array:

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

Here is my code:

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

It is the attender = jUser["attendance"].ToList<Attendance>; line that I have a problem with. It says,

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

How do I access those values?


Solution

  • You have a typo!

    attendance vs attendence

    And this should work

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

    You may find the running result at DotNetFiddle