反序列化json所以我可以数据绑定它

时间:2013-02-04 16:59:11

标签: c# asp.net

我有一个json字符串,我想反序列化并将其放入列表中。我的代码如下,有人可以帮我正确的方向吗?当我运行Response.Write(reports.Count);在我试图反序列化之后,它确实计算了2个条目,但我似乎无法绑定它。有什么建议?

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();

1 个答案:

答案 0 :(得分:1)

试试这个(Main()来自LinqPad,但它应该给你一个想法)

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