我可以從我的控制器獲得以下json輸出,但我需要從myDates & myStaff刪除「key」&「value」,並按約會將約會時間分組 - 例如:date0 ,DATE1,DATE2 ..MVC 4 Linq格式json響應
{
"myDates": [{
"Key": "date0",
"Value": "23/02/2013"
}, {
"Key": "date1",
"Value": "24/02/2013"
}, {
"Key": "date2",
"Value": "25/02/2013"
}, {
"Key": "date3",
"Value": "26/02/2013"
}, {
"Key": "date4",
"Value": "27/02/2013"
}, {
"Key": "date5",
"Value": "28/02/2013"
}, {
"Key": "date6",
"Value": "1/03/2013"
}, {
"Key": "dname0",
"Value": "Saturday"
}, {
"Key": "dname1",
"Value": "Sunday"
}, {
"Key": "dname2",
"Value": "Monday"
}, {
"Key": "dname3",
"Value": "Tuesday"
}, {
"Key": "dname4",
"Value": "Wednesday"
}, {
"Key": "dname5",
"Value": "Thursday"
}, {
"Key": "dname6",
"Value": "Friday"
}, {
"Key": "ndate",
"Value": "2013-03-02"
}, {
"Key": "pdate",
"Value": "2013-02-16"
}],
"myStaff": [{
"Key": "staff0",
"Value": [
[{
"SlotID": 42501,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "23/02/2013",
"SlotDay": "Saturday",
"SlotTime": "10:00"
}, {
"SlotID": 42502,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "23/02/2013",
"SlotDay": "Saturday",
"SlotTime": "10:30"
}],
[{
"SlotID": 47001,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "24/02/2013",
"SlotDay": "Sunday",
"SlotTime": "10:00"
}, {
"SlotID": 47002,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "24/02/2013",
"SlotDay": "Sunday",
"SlotTime": "10:30"
}]
]
}]
}
基本上,我需要的JSON格式如下圖所示:
{
"myDates": [{
"date0": "23/02/2013",
"date1": "24/02/2013",
"date2": "25/02/2013",
"date3": "26/02/2013",
"date4": "27/02/2013",
"date5": "28/02/2013",
"date6": "1/03/2013",
"dname0": "Saturday",
"dname1": "Sunday",
"dname2": "Monday",
"dname3": "Tuesday",
"dname4": "Wednesday",
"dname5": "Thursday",
"dname6": "Friday",
"ndate": "2013-03-02",
"pdate": "2013-02-16",
}],
"myStaff": [{
"staff0": {[
"date0": {[
[{
"SlotID": 42501,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "23/02/2013",
"SlotDay": "Saturday",
"SlotTime": "10:00"
}, {
"SlotID": 42502,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "23/02/2013",
"SlotDay": "Saturday",
"SlotTime": "10:30"
}],
"date1": {[
"SlotID": 47001,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "24/02/2013",
"SlotDay": "Sunday",
"SlotTime": "10:00"
}, {
"SlotID": 47002,
"StaffID": 1,
"BusinessID": 1,
"SlotDate": "24/02/2013",
"SlotDay": "Sunday",
"SlotTime": "10:30"
}]
]}
}]
}]
}
這是我的控制器:
public ActionResult Index(DateTime start, string id = null)
{
var allids = Request.QueryString["id"];
// split the input into anonymous objects containing staffid and businessid
var staffids = from staffid in allids.Split(',').Select(x => x.Split('-'))
select new { sid = int.Parse(staffid[0]), bid = int.Parse(staffid[1]) };
// get the days you need
var days = Enumerable.Range(0, 7).Select(x => start.AddDays(x));
// create myDates
int i = 0;
var myDates = (from day in days
select new KeyValuePair<string, string>(
String.Format("date{0}", i++),
day.ToShortDateString())).ToList();
i = 0;
myDates.AddRange(
(from day in days
select new KeyValuePair<string, string>(
String.Format("dname{0}", i++),
day.DayOfWeek.ToString())).ToList());
myDates.Add(new KeyValuePair<string, string>("ndate", days.First().AddDays(7).ToString("yyyy-MM-dd")));
myDates.Add(new KeyValuePair<string, string>("pdate", days.First().AddDays(-7).ToString("yyyy-MM-dd")));
// receive all the stored_procedures
i = 0;
var myStaff = from staff in staffids
select new KeyValuePair<string, object>(
String.Format("staff{0}", i++),
(from day in days
select db.Database.SqlQuery<GetAvailableAppointments>("EXEC SP_GetAvailableAppointments @StaffID, @BusinessID, @StartDate",
new SqlParameter("StaffID", staff.sid),
new SqlParameter("BusinessID", staff.bid),
new SqlParameter("StartDate", day))).ToList()
);
return Json(new { myDates, myStaff }, JsonRequestBehavior.AllowGet);
}
任何幫助,將不勝感激:)
謝謝您的回答看看我的更新回答您的相關問題,但遺憾的是不工作,因爲它消除所有的日期數字(DATE0, date1.date2..etc)from myDates&從myStaff中刪除所有員工編號(staff0,staff1,staff2..etc),因爲這些設置爲「Keys」。還需要將day.DayOfWeek添加到myStaff json結果。 – MWD 2013-02-23 07:52:02
關於從日期鍵中刪除數字的問題是什麼?我的意思是沒有那些你只有一個數組,你可以通過訪問日期[0] date [1] ...等等 – Ingo 2013-02-24 20:46:59