大家好,希望你们在假期里度过了一个愉快的假期。
我创建了一个 WebService,使用 LINQ/JavaScriptSerializer 返回一个包含城市和城市内公司列表的 JSON 字符串。
我的代码大致如下:
var data = from c in db.Companies
group c by c.City into cities
select new
{
city = cities.Key,
companies = from company in cities
select company.Name
};
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(data);
这将产生以下JSON字符串。
[
{"city":"Auckland","companies":["Company1","Company2"]},
{"city":"Wellington","companies":["Company3","Company4","Company5"]}
]
然而,我希望将城市作为关键字,以便能够轻松地通过它进行搜索。
例如:
[
"Auckland" : {"companies":["Company1","Company2"]},
"Wellington" : {"companies":["Company3","Company4","Company5"]}
]
有什么想法吗?