0

I am able to get the following json output from my controller but I need to remove the "key" & "value" from myDates & myStaff, and group the appointment times by date - eg: date0, date1, date2..

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

basically, I need to get the json formatted like below:

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

This is my controller:

        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);

    }

Any help would be appreciated :)

MWD
  • 1,632
  • 2
  • 18
  • 39

4 Answers4

0

Try this instead :

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

Hope this will help !!

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
  • Thanks for your answer but unfortunately that doesn't work as it removes all the date numbers (date0,date1.date2..etc) from MyDates & removes all the staff numbers (staff0,staff1,staff2..etc) from myStaff as these are setup as the "Keys".. Also need to add the day.DayOfWeek to the myStaff json result. – MWD Feb 23 '13 at 07:52
  • what would be the Problem about removing the numbers from the date keys? i mean without those you just have an Array wich you can walkthrough accessing date[0] date[1] ... etc – Ingo Feb 24 '13 at 20:46
0

This wont give you a [] of items but it's pretty close

...

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

should give a result like:

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

Note: however you should probably do some refactoring to get to a dictionary without these many steps.

Rickard
  • 2,325
  • 13
  • 22
0

You can try converting myDates into a dynamic object like this (it's on the System.Dynamic namespace) :

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

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

And for the serialization part, instead of using the default JSON serializer, try using this serializer (it also available via NuGet), something like this :

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

You can do the similar steps for the myStaff part, so it will produce the result expected.

andri
  • 1,021
  • 1
  • 9
  • 16
0

The key and value names you get using this solution come from using KeyValuePairs. In order to avoid that you should write your own data models that have properties named after the Json-Keys you would expect. To see what i mean have a look at my updated answer to your related question at

MVC 4 controler - using dynamic StaffID's & BusinessID's instead of hard coded ones

Community
  • 1
  • 1
Ingo
  • 1,805
  • 18
  • 31