把List集合转化成Dictionary
public ActionResult Dimo() { Dictionary<string, Object> param = new Dictionary<string, Object>(); param.Add("UID", user.UID); string urlUserPoints = RouteManager.GetApiRoute("User", "GetPointById", param); List<UserPoints> userPoints = JsonConvert.DeserializeObject<UserPoints>(UserList.GetUser(urlUserPoints)); string urlGetIndustriesAndJobs = RouteManager.GetApiRoute("User", "GetIndustriesAndJobs"); Dictionary<string, Object> param2 = JsonConvert.DeserializeObject<Dictionary<string, Object>>(UserList.GetUser(urlGetIndustriesAndJobs)); List<CatalogForIndustries> catalogForIndustries = SerializeHelper.DeserializeFromJson<List<CatalogForIndustries>>(SerializeHelper.SerializeToJson(param2["catalogForIndustries"])); //与第一个不同,第二个传来的是一个Dictionary,这个字典中包含两个集合,所以在反序列化后并无法直接转化成list集合,所以需要再序列化反序列化一次 Dictionary<string, List<CatalogForIndustries>> temp = catalogForIndustries.GroupBy(a => a.ParentID).ToDictionary(a => a.Key, a => a.ToList()); //这个是ToDictionary的写法,把list转化成Dictionary 字典 this.ViewBag.catalogGroup = temp; return view();
} [HttpGet] [SecurityCheck] public HttpResponseMessage GetIndustriesAndJobs(string guid) { List<CatalogForIndustries> industies = BllFactory.Instance.UserBLL.GetCatalogForIndustries(); List<CatalogForJobs> jobs = BllFactory.Instance.UserBLL.GetCatalogForJobs(); Dictionary<string,object> param = new Dictionary<string,object>(); param.Add("catalogForIndustries", industies); param.Add("catalogForJobs", jobs); return ConvertHelper.toJson(param); }
以上是个人经验和理解(有点生涩),写给自己看的,看得懂就用,看不懂不要pen