2016-04-05 70 views
2

我想從我的列表對象中創建具有嵌套對象的json結構。如何從數據列表創建json結構?

這是我的課:

public class Employee 
{ 
    public int EmployeeId { get; set; } 
    public int Skillssetpoints { get; set; } 
    public string Name { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 
} 

public class EmployeeModel 
{ 
    public int EmployeeId { get; set; } 
    public List<int> Skillssetpoints { get; set; } 
    public string Name { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 
} 

記錄是這樣的:

EmployeeId SkillssetPoints Date 
1    10    4/5/2016 16:12:12 
2    12    3/5/2016 17:12:12 
3    4    8/5/2016 8:12:12 
4    20    1/5/2016 2:12:12 

這是怎麼了我得到的數據:

var data=context.Employee.Tolist(); 

獲取數據後,我想創建這個json結構以上數據來自EmployeeModel和返回:

預期輸出:

{"Date":"8-5-2016 08:12:12","SkillssetPoints":[4,10,12,20]} 

在日期字段我將採取最高的日期,以便8-5-2016SkillssetPoints將ORDER BY上升。

如何用我的EmployeeModel類創建此json結構?

+0

你應該清楚地說明你只需要返回1條記錄,並且你希望有人告訴你如何過濾你的員工列表,因此它只顯示「最高日期」 – Ggalla1779

回答

4

添加到NuGet包Newtonsoft.Json然後用一個參考......

string result = JsonConvert.Serialize(data); 

它看起來像你需要的數據在你的數據庫的模型格式做投影轉換第一,然後序列化結果......

var groupedData = data 
    .GroupBy(s => s.EmployeeId) 
    .OrderBy(s => s.Date) 
    .Select(g => new EmployeeModel { 
     EmployeeId = g.Key, 
     Name = g.First().Name, 
     Date = g.First().Date, 
     Skillssetpoints = g.Select(s => s.Skillssetpoints).OrderBy(i => i).ToList() 
    }); 

這應該產生這種模式的集合...

public class EmployeeModel 
{ 
    public int EmployeeId { get; set; } 
    public List<int> Skillssetpoints { get; set; } 
    public string Name { get; set; } 
    public DateTime? Date { get; set; } 
} 

...當我這樣做......

​​

我得到這個輸出...

[ 
    { 
     "EmployeeId": 1, 
     "Skillssetpoints": [1,2,3,4], 
     "Name": "Homer Simpson", 
     "Date": "2016-04-05T11:42:09.9126748+01:00" 
    }, 
    { 
     "EmployeeId": 2, 
     "Skillssetpoints": [1,2,3,4], 
     "Name": "Marge Simpson", 
     "Date": "2016-04-05T11:42:09.9126748+01:00" 
    }, 
    { 
     "EmployeeId": 3, 
     "Skillssetpoints": [1,2,3,4], 
     "Name": "Lisa Simpson", 
     "Date": "2016-04-05T11:42:09.9126748+01:00" 
    }, 
    { 
     "EmployeeId": 4, 
     "Skillssetpoints": [1,2,3,4], 
     "Name": "Bart Simpson", 
     "Date": "2016-04-05T11:42:09.9126748+01:00" 
    } 
] 
+0

請看我的問題,因爲使用你的解決方案是給我輸出是這樣的:[{},{}] –

2

通過在特定的領域分組選擇所需的記錄,然後在需要的圖案准備匿名對象和序列化的最終結果,

 var models = (from em in employeeModels 
         group em by em.ID into g 
         select new 
         { 
          Id = g.Key, 
          maxDate = g.Max(p => p.Date) 
         }).ToList(); 
     var result = new 
     { 
      date = prices.Max(p => p.maxDate), 
      SkillssetPoints = prices.Select(p => p.Id).ToList() 
     }; 

     var json = JsonConvert.SerializeObject(result); 

您將得到JSON的格式,如:

{ 
    "date": "2016-04-05T16:39:54.8420979+05:30", 
    "SkillssetPoints": [ 
     1, 
     2, 
     3 
    ] 
} 
2

您可以嘗試創建一個新的對象,如下序列:

var result = JsonConvert.Serialize (new { 
    Date = context.Employee.Max(e => e.Date), 
    SkillssetPoints = context.Employee.Select(e => e.SkillssetPoints) 
})); 
1

我想你可以試試這個 它正常工作與我。

public ActionResult GetCitiesWithBranches(int regionID) 
    { 
     var cities = 
      _context.Cities.Where(e => e.RegionCode == regionID) 
       .Select(e => new { ID = e.CityCode, Name = e.Name }) 
       .ToList(); 
     return Json(new { cities = cities }); 
    } 

,並在視圖中,我使用的是:

var json = { regionID: data }; 
$.ajax({ 
      type: "POST", 
      url: '@Url.Action("GetCitiesWithBranches", "Admin")', 
      data: json, 
      dataType: "json", 
      error: function (xhr, status, error) { 
       //alert("error routine"); 
      }, 
      success: function (res) {      
       if (res.cities) { 

       } 
      } 
     }); 

希望這將有助於..

+0

這是否適用於Web api控制器:Json(new {cities = cities}); –

+0

我沒有在web api控制器中嘗試它。 –

+0

它在我的MVC應用程序中正常工作 –

2

用牛頓JSON,它可以用的​​NuGet和代碼是非常容易的。

using Newtonsoft.Json; 

var jsonList = JsonConvert.SerializeObject(context.Employee.Tolist()); 

乾杯從NuGet包

2

用牛頓JSON和嘗試此代碼

列表lstEmp =新名單();

 for (int i = 1; i <= 4; i++) 
     { 
      Employee emp = new Employee(); 
      emp.EmployeeId = i; 
      emp.Name = "Name" + i; 
      emp.Skillssetpoints = i + 1; 
      emp.Date = DateTime.Now.AddDays(i); 
      lstEmp.Add(emp); 
     } 

     var data = lstEmp; 
     var result = new EmployeeModel 
     { 
      Date = data.Max(p => p.Date), 
      Skillssetpoints = data.Select(p => p.Skillssetpoints).ToList() 
     }; 

     var JsonData = JsonConvert.SerializeObject(new 
     { 
      Date = result.Date, 
      Skillssetpoints = result.Skillssetpoints 
     }); 

乾杯

2

1)安裝用的NuGet

2 Newtonsoft.Json封裝)在頂部

using Newtonsoft.Json; 

3添加命名空間)添加[JsonIgnore]的模型類的屬性的頂您不希望包含在json轉換中

public class Employee 
{ 
    public int EmployeeId { get; set; } 
    public int Skillssetpoints { get; set; } 
    public string Name { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 
} 

public class EmployeeModel 
{ 
    [JsonIgnore] 
    public int EmployeeId { get; set; } 
    public List<int> Skillssetpoints { get; set; } 
    [JsonIgnore] 
    public string Name { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 
} 

4)最終代碼如下

var data = new List<Employee>(); 
     data.Add(new Employee { EmployeeId = 1, Skillssetpoints = 10, Date = Convert.ToDateTime("4/5/2016 16:12:12") }); 
     data.Add(new Employee { EmployeeId = 2, Skillssetpoints = 12, Date = Convert.ToDateTime("3/5/2016 17:12:12") }); 
     data.Add(new Employee { EmployeeId = 3, Skillssetpoints = 4, Date = Convert.ToDateTime("8/5/2016 8:12:12") }); 
     data.Add(new Employee { EmployeeId = 4, Skillssetpoints = 20, Date = Convert.ToDateTime("1/5/2016 2:12:12") }); 

     var highestDate = data.OrderByDescending(e => e.Date).First().Date; 
     var skillssetpointsList = data.Select(e => e.Skillssetpoints).ToList(); 


     EmployeeModel employeeModel = new EmployeeModel() 
     { 
      Date = highestDate, 
      Skillssetpoints = skillssetpointsList 
     }; 

     string jsonString = JsonConvert.SerializeObject(employeeModel); 

現在,jsonString = { 「Skillssetpoints」:[10,12,4,20], 「日期」: 「2016-05-08T08:12:12」 }

相關問題