How can I get JsonResult to return an array of arrays (without field names) and not an array of objects?

I have an IEnumerable list of date / value pairs that I return as a Json list for the fleet. However, when I call JsonResult (), the result is as follows:

[{"Date":date1, "Value":value1}, {"Date":date2, "Value":value2}...] 

Fleet expected

 [[date1, value1], [date2, value2]...] 

Is there any easy way to get the MVC structure to output such objects, or do I need to write my own seralizer / StringBuffer code? In this case, I don’t even need to display the field names, only the values ​​themselves.

+4
source share
3 answers

It looks like you just need to return the string like this:

 var builder = new StringBuilder(); builder.Append("["); foreach (var item in listOfDateTimes) builder.AppendFormat("[{0}, {1}], ", item.Key, item.Value); var result = builder.ToString().TrimEnd(new char[]{',',' '}) + "]"; return result; 
+1
source

Are these date / value pairs of type System.Web.UI.Pair ? If so, you can do this:

 return Json(yourIEnumerable.Select(x => new[] { x.First, x.Second }).ToArray()); 

It returns what you want

 [["\/Date(1255686550232)\/","foo"],["\/Date(1255686550232)\/","bar"]] 

Even if they are not a type of System.Web.UI.Pair , I'm sure you understand this idea.

+5
source

It’s best to write it yourself, but this is a trivial exercise.

In my opinion, it’s worth spending an hour looking for a way to do this, when you can spend 10 minutes and just serialize yourself.

+1
source

All Articles