C#,Json转DataTable,DataTable转Json

Json格式

[{"id":"329","txt":"luofenming.com"},{"id":"338","txt":"lqwvje"},{"id":"888","txt":"78630559"}]

System.Web.Extensions 引用这个程序集


        /// <summary>
        /// Json转DataTable
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public  DataTable Json2DataTable(string json)
        {
            DataTable dataTable = new DataTable();
            DataTable result = null;
            try
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue;
                ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);

                if (arrayList.Count > 0)
                {
                    foreach (Dictionary<string, object> dictionary in arrayList)
                    {
                        if (dictionary.Keys.Count<string>() == 0)
                        {
                            result = dataTable;
                            return result;
                        }
                        if (dataTable.Columns.Count == 0)
                        {
                            foreach (string current in dictionary.Keys)
                            {
                                dataTable.Columns.Add(current, dictionary[current].GetType());
                            }
                        }
                        DataRow dataRow = dataTable.NewRow();
                        foreach (string current in dictionary.Keys)
                        {
                            dataRow[current] = dictionary[current];
                        }
                        dataTable.Rows.Add(dataRow);  //这里datarow都有值,但是datatable没加上
                    }
                }
            }
            catch { };
            result = dataTable;
            return result;

        }

        /// <summary>    
        /// DataTable转Json    
        /// </summary>    
        /// <param name="dtb"></param>    
        /// <returns></returns>    
        private string DataTable2Json(DataTable dtb)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            javaScriptSerializer.MaxJsonLength = Int32.MaxValue;
            ArrayList dic = new ArrayList();
            foreach (DataRow row in dtb.Rows)
            {
                Dictionary<string, object> drow = new Dictionary<string, object>();
                foreach (DataColumn col in dtb.Columns)
                {
                    drow.Add(col.ColumnName, row[col.ColumnName]);
                }
                dic.Add(drow);
            }
            return javaScriptSerializer.Serialize(dic);
        }
以下是调用互转方法
        private void button1_Click(object sender, EventArgs e)
        {
            string json = "[{\"id\":\"329\",\"txt\":\"luofenming.com\"}, {\"id\":\"338\",\"txt\":\"lqwvje\"},{\"id\":\"888\",\"txt\":\"78630559\"}]";

            DataTable dt = Json2DataTable(json);//Json转DataTable
            string ss = DataTable2Json(dt);//dataTable转Json
        }


评论