C# convert data in service result

 static DataTable SplitJsonValueToDataTable(string postResult)
        {
            DataTable dt = new DataTable();
            try
            {
                string[] dataArr = postResult.Split(new string[] { "," }, 3, StringSplitOptions.None);
                string[] firstDataArr = dataArr[0].Split(new string[] { ":" }, StringSplitOptions.None);
                if (!string.Equals(firstDataArr[1], "0"))
                {
                    return dt;
                }
                string dataValue = dataArr[2].Split(new string[] { ":" },2,StringSplitOptions.None)[1]. Replace("[", "").Replace("]", "");
                string[] dataValueArr = dataValue.Split(new string[] { "}" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Replace("{", "")).ToArray();
                if (dataValueArr != null && dataValueArr.Any())
                {
                    string[] firstDataValueArrItem = dataValueArr[0].Split(new string[] { "," }, StringSplitOptions.None);
                    foreach (var a in firstDataValueArrItem)
                    {
                        string colName = a.Split(new string[] { ":" }, StringSplitOptions.None)[0].Replace(@"\""", " ").Replace(@"""", "");
                        dt.Columns.Add(colName);
                    }
                    foreach (var dva in dataValueArr)
                    {
                        DataRow dr = dt.NewRow();
                         
                        string[] arr = dva.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        if (arr != null && arr.Any())
                        {
                            for (int i = 0; i < arr.Length; i++)
                            {
                                dr[i] = arr[i].Split(new string[] { ":" }, StringSplitOptions.None)[1].Replace(@"\""", " ").Replace(@"""", "");
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }
            catch
            {
            }
            return dt;
        }

 

posted @ 2020-03-20 18:11  FredGrit  阅读(198)  评论(0编辑  收藏  举报