2013-02-23 95 views
0

我可以從我的控制器獲得以下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); 

    } 

任何幫助,將不勝感激:)

回答

0

試試這個:

return Json(new { 
        myDates.Select(D => D.Value), 
        myStaff.Select(S => S.Value) 
       }, JsonRequestBehavior.AllowGet); 

希望這將幫助!

+0

謝謝您的回答看看我的更新回答您的相關問題,但遺憾的是不工作,因爲它消除所有的日期數字(DATE0, date1.date2..etc)from myDates&從myStaff中刪除所有員工編號(staff0,staff1,staff2..etc),因爲這些設置爲「Keys」。還需要將day.DayOfWeek添加到myStaff json結果。 – MWD 2013-02-23 07:52:02

+0

關於從日期鍵中刪除數字的問題是什麼?我的意思是沒有那些你只有一個數組,你可以通過訪問日期[0] date [1] ...等等 – Ingo 2013-02-24 20:46:59

0

這不會給你一個[項目],但它是非常接近

... 

var dictionary = myDates.ToDictionary(x => x.Key, x => x.Value); 
return Json(new { myDates = dictionary, myStaff }, JsonRequestBehavior.AllowGet); 

應該給像一個結果:

{ 
    "myDates": { 
     "date0": "2013-02-23", 
     "date1": "2013-02-24", 
     "date2": "2013-02-25", 
     "date3": "2013-02-26", 
     ... 
    }, 
    "myStaff": {...} 
} 

注:但你或許應該做一些重構,以獲得一個字典沒有這麼多的步驟。

0

您可以嘗試轉換myDates成這樣一個dynamic對象(它是在System.Dynamic命名空間):

dynamic myDynamicDates = new ExpandoObject(); 
var d = myDynamicDates as IDictionary<string, object>; 

foreach (var myDate in myDates) 
{ 
    d[myDate.Key] = myDate.Value; 
} 

而對於序列化的一部分,而不是使用默認的JSON序列化,嘗試使用this serializer(也通過NuGet可用),像這樣:

return Content(JsonConvert.SerializeObject(new { myDates = new []{ myDynamicDates } }), "application/json"); 

你可以做的myStaff部分相似的步驟,所以它會產生結果前pected。