Пример #1
0
        private stComments addComments(List <string> lst, int idx, int idxM)
        {
            stComments newst = new stComments();

            List <string> Nlist = lst.GetRange(idx, idxM - idx);

            var sourceProperties = typeof(stComments).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(newst, ivalue, null);
                }
                if (info.PropertyType == typeof(string))
                {
                    string svalue = value;
                    info.SetValue(newst, svalue, null);
                }
            }

            return(newst);
        }
Пример #2
0
        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 + "]");
        }