2

我有一个 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();
4

1 回答 1

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; }
}
于 2013-02-04T17:10:26.527 回答