将JSON项放入C#中的WPF GRID

时间:2015-02-23 16:35:20

标签: c# json wpf

如何将项目放在WPF网格中的JSON字符串中?

{  
    "success":"true",
    "response":{  
        "Page":1,
        "PageFirst":1,
        "PageLast":1,
        "PageRecordFirst":1,
        "PageRecordLast":2147483647,
        "PageSize":2147483647,
        "RecordCount":2,
        "ResultSet":[  
            {  
                "CompanyId":1,
                "CompanyName":"Focus",
                "ComputedProjectProgressAll":39.000000,
                "ComputedProjectProgressCurrent":39.000000,
                "ComputedProjectProgressExpected":86.00,
                "ComputedTaskCountAll":434,
                "ComputedTaskCountCurrent":354,
                "CreateDate":"\/Date(1421947846600-0800)\/",
                "CreateUserId":1,
                "CreateUserName":"MobiCloud Admin",
                "CustomerId":1,
                "CustomerName":"MobiCloud",
                "Description":"Obra 001",
                "Id":7,
                "IsActive":true,
                "ModifyDate":"\/Date(1421947846600-0800)\/",
                "ModifyUserId":1,
                "ModifyUserName":"MobiCloud Admin",
                "Name":"Obra Desenv 001",
                "Status":0
            },
            {  
                "CompanyId":1,
                "CompanyName":"Focus",
                "ComputedProjectProgressAll":69.000000,
                "ComputedProjectProgressCurrent":69.000000,
                "ComputedProjectProgressExpected":100.00,
                "ComputedTaskCountAll":199,
                "ComputedTaskCountCurrent":199,
                "CreateDate":"\/Date(1422298868660-0800)\/",
                "CreateUserId":1,
                "CreateUserName":"MobiCloud Admin",
                "CustomerId":1,
                "CustomerName":"MobiCloud",
                "Description":"sadasdsad",
                "Id":8,
                "IsActive":true,
                "ModifyDate":"\/Date(1422298868660-0800)\/",
                "ModifyUserId":1,
                "ModifyUserName":"MobiCloud Admin",
                "Name":"Obra Desenv 002",
                "Status":0
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您需要创建一个等效于JSON的c#类。使用http://json2csharp.com/创建c#类。然后,您需要使用http://json.codeplex.com/中的JSON.NET反序列化您的JSON字符串。现在你已经完成了所有的事情。现在,您可以将这些数据分配到WPF控件中。我已将响应结果集分配到一个数据网格中。请参阅以下代码。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        LoadData();
    }

    private void LoadData()
    {
        string str = File.ReadAllText("1.txt");
        RootObject deserializedObject = JsonConvert.DeserializeObject<RootObject>(str);
        dgr.ItemsSource = deserializedObject.response.ResultSet;
    }
}

public class ResultSet
{
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }
    public double ComputedProjectProgressAll { get; set; }
    public double ComputedProjectProgressCurrent { get; set; }
    public double ComputedProjectProgressExpected { get; set; }
    public int ComputedTaskCountAll { get; set; }
    public int ComputedTaskCountCurrent { get; set; }
    public DateTime CreateDate { get; set; }
    public int CreateUserId { get; set; }
    public string CreateUserName { get; set; }
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
    public string Description { get; set; }
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public DateTime ModifyDate { get; set; }
    public int ModifyUserId { get; set; }
    public string ModifyUserName { get; set; }
    public string Name { get; set; }
    public int Status { get; set; }
}

public class Response
{
    public int Page { get; set; }
    public int PageFirst { get; set; }
    public int PageLast { get; set; }
    public int PageRecordFirst { get; set; }
    public long PageRecordLast { get; set; }
    public long PageSize { get; set; }
    public int RecordCount { get; set; }
    public List<ResultSet> ResultSet { get; set; }
}

public class RootObject
{
    public string success { get; set; }
    public Response response { get; set; }
}
 <Grid>
    <DataGrid x:Name="dgr"/>
</Grid>