How to return JSON in a specific format in ASP.NET MVC using Json () without property names

I use the javascript graphics library that expects its data in a specific JSON format - without property names. I have an object in my model that I use to return data to charts. It looks like this:

public class ChartData
{
    public string Key { get; set; }
    public int Value { get; set; }
}

The action is as follows:

public ActionResult AssetsPerFloor(Guid id)
    {
        var results = from a in surveyRepository.GetAssetsForBuidling(id)
                      group a by a.Room.Floor into g
                      select new ChartData{ Key = g.Key.ToString(), Value = g.Count() };
        return Json(results);
    }

This returns JSON in the format [{"Key":"Main Building","Value":1}]

However, the diagram does not require property names, for example: [[5, 2], [6, 3], [8, 2]]

In any case, I can return the results in this format. I'm sure there is a simple trick, but I can't think about it.

0
source share
1 answer

As I understand it, it needs to return a multidimensional array. Try the following:

var results = 
    (from a in surveyRepository.GetAssetsForBuidling(id)
        group a by a.Room.Floor into g
        select new ChartData{ Key = g.Key.ToString(), Value = g.Count() })
        .Select(x => new string[] { x.Key, x.Value.ToString() };
return Json(results);
+1

All Articles