private stUser addUser(List <string> lst, int idx, int idxM) { stUser st_User = new stUser(); st_User.blogs = new List <stBlogs>(); List <string> Nlist = lst.GetRange(idx, idxM - idx); var sourceProperties = typeof(stUser).GetProperties(); foreach (PropertyInfo info in sourceProperties) { string Nm = info.Name; Type type = info.PropertyType; string Row = Nlist.Find(n => n.Contains(Nm)); if (string.IsNullOrEmpty(Row)) { continue; } string[] spRow = Row.Split(':'); string value = spRow[spRow.Length - 1].Replace("/t", "") .Replace("]", "") .Replace("}", "") .Replace('"', ' ') .Trim(); if (info.PropertyType == typeof(int)) { int ivalue = int.Parse(value); info.SetValue(st_User, ivalue, null); } if (info.PropertyType == typeof(string)) { string svalue = value; info.SetValue(st_User, svalue, null); } } return(st_User); }
private string WorkJson(string sJosn) { List <string> list = sJosn.Split(',').ToList(); List <int> matchingIndexes = (from current in list.Select((value, index) => new { value, index }) where current.value.Contains("_entity") select current.index).ToList(); matchingIndexes.Add(list.Count); List <stUser> lUser = new List <stUser>(); List <stBlogs> lBlogs = new List <stBlogs>(); List <stComments> lComments = new List <stComments>(); for (int i = 0; matchingIndexes.Count - 1 > i; i++) { string _entity = list[matchingIndexes[i]]; if (list[matchingIndexes[i]].Contains("User")) { stUser nUser = addUser(list, matchingIndexes[i], matchingIndexes[i + 1]); if (!lUser.Exists(u => u.id == nUser.id)) { lUser.Add(nUser); } } if (list[matchingIndexes[i]].Contains("Blog")) { lBlogs.Add(addBlogs(list, matchingIndexes[i], matchingIndexes[i + 1], lUser[lUser.Count - 1].id)); } if (list[matchingIndexes[i]].Contains("Comment")) { stComments comments = addComments(list, matchingIndexes[i], matchingIndexes[i + 1]); i++; stUser nUserCom = addUser(list, matchingIndexes[i], matchingIndexes[i + 1]); if (!lUser.Exists(u => u.id == nUserCom.id)) { lUser.Add(nUserCom); } comments.UserId = nUserCom.id; comments.blogId = lBlogs[lBlogs.Count - 1].id; lComments.Add(comments); } } string rJson = ""; lUser = lUser.OrderBy(o => o.id).ToList(); rJson = "\"Users\":" + JsonConvert.SerializeObject(lUser); rJson += ",\"blogs\":" + JsonConvert.SerializeObject(lBlogs); rJson += ",\"Comments\":" + JsonConvert.SerializeObject(lComments); return("[" + rJson + "]"); }