0

I am using a charting javascript library that expects its data in a specific JSON format - without property names. I have an object in my Model that I am using to return the data to the charts. This looks as follows:

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

An action looks 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 chart requires no property names, eg: [[5, 2], [6, 3], [8, 2]]

Is there anyway I can return the results in this format. I'm sure there's a simple trick to it, but I cant think of it.

1 Answer 1

1

As far as I understand, it needs to return a multi-dimensional array. Try this :

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.