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

最佳答案

试试这个(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; }
}

关于c# - 反序列化json,以便我可以将其数据绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14691567/

10-13 07:49