2015-02-23 51 views
-4

如何將項目放入WPF網格中的JSON字符串中?把JSON項目放到C#中的WPF網格中#

{ 
    "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 
      } 
     ] 
    } 
} 
+3

erm ..請英語。請格式化。 – 2015-02-23 16:35:56

+2

即使在英文中,這是一個可怕的問題... – David 2015-02-23 16:37:27

回答

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> 
+0

var objeto = JsonConvert.DeserializeObject (strJSON); – 2015-02-25 14:50:07