linq.js: GroupBy(), then ToJSON()

心不动则不痛 提交于 2019-12-11 10:31:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!