private Category CreateCategory(int id, JObject json) { Category c = new Category(); c.Id = id; c.Name = json["0"].ToString(); c.Parameters = new List<Parameter>(); foreach (KeyValuePair<string, JToken> x in json) if (x.Key[0] == '_') c.Parameters.Add(CreateParameter(Int32.Parse(x.Key.Substring(1)), toJObject(x.Value))); return c; }
private List<CategoryGroup> ParseJson(string jsonRawData, string html) { JObject json = JObject.Parse(jsonRawData); List<CategoryGroup> list = new List<CategoryGroup>(); List<Category> lc = new List<Category>(); foreach (KeyValuePair<string, JToken> k1 in json) { if (k1.Value.Type == JTokenType.Array) { list.Add(new CategoryGroup { Id = Int32.Parse(k1.Key), Name = ((JArray)k1.Value)[0].ToString() }); } else { lc.Add(CreateCategory(Int32.Parse(k1.Key), (JObject)k1.Value)); } } HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode root = doc.GetElementbyId("fld_category_id"); foreach (HtmlNode x in root.ChildNodes) { if (x.Name != "optgroup") continue; string label = x.GetAttributeValue("label", ""); CategoryGroup group = list.First(p => p.Name == label); group.Categories = new List<Category>(); foreach (HtmlNode y in x.ChildNodes) { if (y.Name != "option") continue; int catId = y.GetAttributeValue("value", -1); Category c; try { c = lc.First(p => p.Id == catId); } catch (Exception e) { c = new Category(); c.Id = catId; c.Name = y.NextSibling.InnerText; } group.Categories.Add(c); } } list = list.Where(p => p.Categories != null).ToList(); return list; }