乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      json和table互相轉(zhuǎn)換

       實(shí)力決定地位 2012-05-31

      --------網(wǎng)絡(luò)摘取--------

      using System;
      using System.Data;
      using System.Configuration;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Web.UI.HtmlControls;
      using System.Data;
      using System.Text.RegularExpressions;
      using System.Collections;
      using System.Text;

      namespace Public
      {
      /// <summary>
      /// JsonAndDateTable 的摘要說明
      /// </summary>
      public static class JsonAndDateTable
      {

      /// <summary>
      /// 根據(jù)Json返回DateTable,JSON數(shù)據(jù)格式如:
      /// {table:[{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]}
      /// </summary>
      /// <param name="strJson">Json字符串</param>
      /// <returns></returns>
      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("]"));

      //獲取數(shù)據(jù)
      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(',');

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

      //增加內(nèi)容
      DataRow dr = tb.NewRow();
      for (int r = 0; r < strRows.Length; r++)
      {
      dr[r] = strRows[r].Split(':')[1].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", "");
      }
      tb.Rows.Add(dr);
      tb.AcceptChanges();
      }

      return tb;
      }


      /// <summary>
      /// 根據(jù)DataTable返回JSON數(shù)據(jù)格式如:
      /// {table:[{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]}
      /// </summary>
      /// <param name="tb">需要轉(zhuǎn)換的表</param>
      /// <returns></returns>
      public static string DateTableToJson(DataTable tb)
      {
      if (tb == null || tb.Rows.Count == 0)
      {
      return "";
      }

      string strName = "table";
      /*
      string strName = tb.TableName;
      if (strName.Trim().Length == 0)
      {
      strName = "table";
      }*/
      StringBuilder sbJson = new StringBuilder();
      sbJson.Append("{");
      sbJson.Append("\"" + strName + "\":[");
      Hashtable htColumns = new Hashtable();
      for (int i = 0; i < tb.Columns.Count; i++)
      {
      htColumns.Add(i, tb.Columns[i].ColumnName.Trim());
      }

      for (int j = 0; j < tb.Rows.Count; j++)
      {
      if (j != 0)
      {
      sbJson.Append(",");
      }
      sbJson.Append("{");
      for (int c = 0; c < tb.Columns.Count; c++)
      {
      sbJson.Append(htColumns[c].ToString() + ":\"" + tb.Rows[j][c].ToString().Replace(",", ",").Replace(":", ":").Replace("\r\n", " ") + "\",");
      }
      sbJson.Append("index:" + j.ToString()); //序號(hào)
      sbJson.Append("}");
      }
      sbJson.Append("]}");
      return sbJson.ToString();
      }
      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多