JSON与DataTable互转

JSON与DataTable互转直接互转处理

public static string DataTableToJson(DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + dt.Rows[i][j].ToString());

                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]");
            return Json.ToString();
        }
public static DataTable JsonToDataTable(string strJson)
         {
             //取出表名  
              /*
             Regex rg = new Regex(@"(?<={)[^:]+(?=:/[)", RegexOptions.IgnoreCase);
             string strName = rg.Match(strJson).Value;
             DataTable tb = null;
             //去除表名  
            strJson = strJson.Substring(strJson.IndexOf("[") + 1);
             strJson = strJson.Substring(0, strJson.IndexOf("]"));
             */

             DataTable tb = null;
             //获取数据  
            Regex rg = new Regex(@"(?<={)[^}]+(?=})");
             MatchCollection mc = rg.Matches(strJson);
             for (int i = 0; i < mc.Count; i++)
             {
                 string strRow = mc[i].Value;
                 string[] strRows = strRow.Split(',');

                 //创建表  
                if (tb == null)
                 {
                     tb = new DataTable();
                     tb.TableName = "json";
                     foreach (string str in strRows)
                     {
                         DataColumn dc = new DataColumn();
                         string[] strCell = str.Split(':');
                         dc.ColumnName = strCell[0].ToString();
                         tb.Columns.Add(dc);
                     }
                     tb.AcceptChanges();
                 }

                 //增加内容  
                DataRow dr = tb.NewRow();
                 for (int r = 0; r < strRows.Length; r++)
                 {
                     string ls_data;
                     ls_data=strRows[r];

                     ls_data=strRows[r].Split(':')[1].Trim();

                     ls_data=ls_data.Replace("", ",");
                     ls_data=ls_data.Replace("", ":");
                     ls_data=ls_data.Replace("//","");

                     dr[r] =ls_data;
                     
                 }
                 tb.Rows.Add(dr);
                 tb.AcceptChanges();
             }

             return tb;
         }
 

测试了下 还比较结实的!

posted @ 2012-12-25 17:55  AllanChen  阅读(593)  评论(0编辑  收藏  举报