JK36
JK36

Reputation: 853

Deserialize json so I can databind to it

I've got a json string, which I want to deserialize and put it in an list. I've got my code below, can someone help me in the right direction please? When I run Response.Write(reports.Count); after I've tried to deserialise, it does count 2 entries, but I cant seem to bind to it. Any advice?

public class Report
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}



on page_load.....

    responseData = [{"FirstName":"George","LastName":"Clooney"},{"FirstName":"Brad","LastName":"Pitt"}]

    IList<Report> reports = new JavaScriptSerializer().Deserialize<IList<Report>>(responseData);

    Response.Write(reports.Count);

    ReportRepeater.DataSource = reports;
    ReportRepeater.DataBind();

Upvotes: 2

Views: 137

Answers (1)

Bala R
Bala R

Reputation: 108947

Try this (The Main() is from LinqPad but it should give you an idea)

void Main()
{
    string responseData = "[{\"FirstName\":\"George\",\"LastName\":\"Clooney\"},{\"FirstName\":\"Brad\",\"LastName\":\"Pitt\"}]";

    Report[] reports = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Report[]>(responseData);


    reports.Dump(); // <-- Dump() is  another LinqPad extension method that can be ignored.
}


public class Report
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Upvotes: 1

Related Questions