问题
I'm new to linq.js. I'd like to do a GroupBy(), which is then converted to JSON. However, I'm getting back a string array.
var data = [ { "Gender":"M" }, { "Gender":"M" }, { "Gender":"F" } ];
var grouped_dt = Enumerable.From(data).GroupBy("$.Gender", "", 'key,e=>key+":"+e.Count()', "").ToJSON();
My result then looks something like this: [ "M:2", "F:1" ], which hardly looks like JSON (furthermore it is a string; I can alert() it immediately).
Does anybody have any idea where in my syntax am I screwing up? A confession: the data is just a simplified version of the data that I am getting from my server via AJAX, but it is completely in JSON format.
回答1:
Turns out that I'd need to specify a key for each value (following the actual JSON format).
var grouped_dt = Enumerable.From(data).GroupBy("$.Gender", "key,e=>{name:key,y:e.Count()}", "").ToJSON();
Having done this, I'd also need to
var jsonData = $.parseJSON(grouped_dt);
to convert it into a JSON object for use.
来源:https://stackoverflow.com/questions/12017806/linq-js-groupby-then-tojson