NET读取和写入Json文件

By admin in 4858.com on 2019年4月10日
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using System.Text;  
  9. using Newtonsoft.Json;  
  10. using com.zjpx.model;  
  11. using System.Collections;  
  12. using Newtonsoft.Json.Converters;  
  13.   
  14. namespace web  
  15. {  
  16.     public partial class testJson : System.Web.UI.Page  
  17.     {  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.             ConfigFileModel cfm = new ConfigFileModel();  //实体模型类  
  21.             cfm.CreateDate = “2012-02-23”;  
  22.             cfm.FileName = “test.txt”;  
  23.   
  24.             //能够操作集合  
  25.             Hashtable ht = new Hashtable();  
  26.             ht.Add(“hs_name”, “alex”);  
  27.             ht.Add(“hs_pwd”, “ggg”);  
  28.   
  29.             //序列化  
  30.             string js1 = JsonConvert.SerializeObject(cfm);  
  31.             string js2 = JsonConvert.SerializeObject(ht);  
  32.             Response.Write(js1);  
  33.             Response.Write(“<br/>”);  
  34.             Response.Write(js2);  
  35.             //”{\”FileName\”:\”test.txt\”,\”CreateDate\”:\”2012-02-23\”}”  
  36.             //”{\”hs_pwd\”:\”ggg\”,\”hs_name\”:\”alex\”}”  
  37.   
  38.             //反系列化  
  39.             ConfigFileModel debc1 = JsonConvert.DeserializeObject<ConfigFileModel>(js1);  
  40.             ConfigFileModel debc2 = JsonConvert.DeserializeObject<ConfigFileModel>(js2);  
  41.   
  42.             //找到服务器物理路径  
  43.             //string serverAppPath = Request.PhysicalApplicationPath.ToString();  
  44.             string serverAppPath = @”d:\”;  
  45.             //构成配置文件路径  
  46.             string con_file_path =@””+ serverAppPath + @”config.json”;  
  47.   
  48.   
  49.             if (!File.Exists(con_file_path))  
  50.             {  
  51.                 File.Create(con_file_path);  
  52.             }  
  53.   
  54.         //把模型数据写到文件  
  55.             using (StreamWriter sw = new StreamWriter(con_file_path))  
  56.             {  
  57.                 try  
  58.                 {  
  59.   
  60.                     JsonSerializer serializer = new JsonSerializer();  
  61.                     serializer.Converters.Add(new JavaScriptDateTimeConverter());  
  62.                     serializer.NullValueHandling = NullValueHandling.Ignore;  
  63.   
  64.                     //创设Json.net的写入流  
  65.                     JsonWriter writer = new JsonTextWriter(sw);  
  66.                     //把模型数据种类化并写入Json.net的JsonWriter流中  
  67.                     serializer.Serialize(writer, cfm);  
  68.                     //ser.Serialize(writer, ht);  
  69.                     writer.Close();  
  70.                     sw.Close();  
  71.   
  72.                 }  
  73.   
  74.         }  
  75.             catch (Exception ex)  
  76.             {  
  77.                 ex.Message.ToString();  
  78.             }  
  79.   
  80.              //读取json文件  
  81.              using (StreamReader sr = new StreamReader(con_file_path))  
  82.              {  
  83.           try  
  84.                   {  
  85.                       JsonSerializer serializer = new JsonSerializer();  
  86.                       serializer.Converters.Add(new JavaScriptDateTimeConverter());  
  87.                       serializer.NullValueHandling = NullValueHandling.Ignore;  
  88.   
  89.                       //创设Json.net的读取流  
  90.                       JsonReader reader = new JsonTextReader(sr);  
  91.                       //对读取出的Json.net的reader流举办反体系化,并装载到模型中  
  92.                       cfm = serializer.Deserialize<ConfigFileModel>(reader);  
  93.   
  94.                       Response.Write(“<br/>”);  
  95.                       Response.Write(cfm.FileName+”, “+cfm.CreateDate);  
  96.           }  
  97.                   catch (Exception ex)  
  98.                   {  
  99.                       ex.Message.ToString();  
  100.                   }  
  101.               }  
  102.   
  103.         }  
  104.     }  
  105.   
  106.     public class ConfigFileModel  
  107.     {  
  108.         public ConfigFileModel() { }  
  109.   
  110.         string _fileName;  
  111.   
  112.         public string FileName  
  113.         {  
  114.             get { return _fileName; }  
  115.             set { _fileName = value; }  
  116.         }  
  117.         string _createDate;  
  118.   
  119.         public string CreateDate  
  120.         {  
  121.             get { return _createDate; }  
  122.             set { _createDate = value; }  
  123.         }  
  124.     }  
  125. }  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using System.Text;  
  9. using Newtonsoft.Json;  
  10. using com.zjpx.model;  
  11. using System.Collections;  
  12. using Newtonsoft.Json.Converters;  
  13.   
  14. namespace web  
  15. {  
  16.     public partial class testJson : System.Web.UI.Page  
  17.     {  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.             ConfigFileModel cfm = new ConfigFileModel();  //实人体模型型类  
  21.             cfm.CreateDate = “2012-02-23”;  
  22.             cfm.FileName = “test.txt”;  
  23.   
  24.             //能够操作集合  
  25.             Hashtable ht = new Hashtable();  
  26.             ht.Add(“hs_name”, “alex”);  
  27.             ht.Add(“hs_pwd”, “ggg”);  
  28.   
  29.             //序列化  
  30.             string js1 = JsonConvert.SerializeObject(cfm);  
  31.             string js2 = JsonConvert.SerializeObject(ht);  
  32.             Response.Write(js1);  
  33.             Response.Write(“<br/>”);  
  34.             Response.Write(js2);  
  35.             //”{\”FileName\”:\”test.txt\”,\”CreateDate\”:\”2012-02-23\4858.com ,”}”  
  36.             //”{\”hs_pwd\”:\”ggg\”,\”hs_name\”:\”alex\”}”  
  37.   
  38.             //反类别化  
  39.             ConfigFileModel debc1 = JsonConvert.DeserializeObject<ConfigFileModel>(js1);  
  40.             ConfigFileModel debc2 = JsonConvert.DeserializeObject<ConfigFileModel>(js2);  
  41.   
  42.             //找到服务器物理路径  
  43.             //string serverAppPath = Request.PhysicalApplicationPath.ToString();  
  44.             string serverAppPath = @”d:\”;  
  45.             //构成配置文件路径  
  46.             string con_file_path =@””+ serverAppPath + @”config.json”;  
  47.   
  48.   
  49.             if (!File.Exists(con_file_path))  
  50.             {  
  51.                 File.Create(con_file_path);  
  52.             }  
  53.   
  54.         //把模型数据写到文件  
  55.             using (StreamWriter sw = new StreamWriter(con_file_path))  
  56.             {  
  57.                 try  
  58.                 {  
  59.   
  60.                     JsonSerializer serializer = new JsonSerializer();  
  61.                     serializer.Converters.Add(new JavaScriptDateTimeConverter());  
  62.                     serializer.NullValueHandling = NullValueHandling.Ignore;  
  63.   
  64.                     //构建Json.net的写入流  
  65.                     JsonWriter writer = new JsonTextWriter(sw);  
  66.                     //把模型数据系列化并写入Json.net的JsonWriter流中  
  67.                     serializer.Serialize(writer, cfm);  
  68.                     //ser.Serialize(writer, ht);  
  69.                     writer.Close();  
  70.                     sw.Close();  
  71.   
  72.                 }  
  73.   
  74.         }  
  75.             catch (Exception ex)  
  76.             {  
  77.                 ex.Message.ToString();  
  78.             }  
  79.   
  80.              //读取json文件  
  81.              using (StreamReader sr = new StreamReader(con_NET读取和写入Json文件。file_path))  
  82.              {  
  83.           try  
  84.                   {  
  85.                       JsonSerializer serializer = new JsonSerializer();  
  86.                       serializer.Converters.Add(new JavaScriptDateTimeConverter());  
  87.                       serializer.NullValueHandling = NullValueHandling.Ignore;  
  88.   
  89.                       //创设Json.net的读取流  
  90.                       JsonReader reader = new JsonTextReader(sr);  
  91.                       //对读取出的Json.net的reader流实行反体系化,并装载到模型中  
  92.                       cfm = serializer.Deserialize<ConfigFileModel>(reader);  
  93.   
  94.                       Response.Write(“<br/>”);  
  95.                       Response.Write(cfm.FileName+”, “+cfm.CreateDate);  
  96.           }  
  97.                   catch (Exception ex)  
  98.                   {  
  99.                       ex.Message.ToString();  
  100.                   }  
  101.               }  
  102.   
  103.         }  
  104.     }  
  105.   
  106.     public class ConfigFileModel  
  107.     {  
  108.         public ConfigFileModel() { }  
  109.   
  110.         string _fileName;  
  111.   
  112.         public string FileName  
  113.         {  
  114.             get { return _fileName; }  
  115.             set { _fileName = value; }  
  116.         }  
  117.         string _createDate;  
  118.   
  119.         public string CreateDate  
  120.         {  
  121.             get { return _createDate; }  
  122.             set { _createDate = value; }  
  123.         }  
  124.     }  
  125. }  

Json.NET读取和写入Json文件,json.net写入json

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using System.Text;  
  9. using Newtonsoft.Json;  
  10. using com.zjpx.model;  
  11. using System.Collections;  
  12. using Newtonsoft.Json.Converters;  
  13.   
  14. namespace web  
  15. {  
  16.     public partial class testJson : System.Web.UI.Page  
  17.     {  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.             ConfigFileModel cfm = new ConfigFileModel();  //实人体模型型类  
  21.             cfm.CreateDate = “2012-02-23”;  
  22.             cfm.FileName = “test.txt”;  
  23.   
  24.             //能够操作集合  
  25.             Hashtable ht = new Hashtable();  
  26.             ht.Add(“hs_name”, “alex”);  
  27.             ht.Add(“hs_pwd”, “ggg”);  
  28.   
  29.             //序列化  
  30.             string js1 = JsonConvert.SerializeObject(cfm);  
  31.             string js2 = JsonConvert.SerializeObject(ht);  
  32.             Response.Write(js1);  
  33.             Response.Write(“<br/>”);  
  34.             Response.Write(js2);  
  35.             //”{\”FileName\”:\”test.txt\”,\”CreateDate\”:\”2012-02-23\”}”  
  36.             //”{\”hs_pwd\”:\”ggg\”,\”hs_name\”:\”alex\”}”  
  37.   
  38.             //反类别化  
  39.             ConfigFileModel debc1 = JsonConvert.DeserializeObject<ConfigFileModel>(js1);  
  40.             ConfigFileModel debc2 = JsonConvert.DeserializeObject<ConfigFileModel>(js2);  
  41.   
  42.             //找到服务器物理路径  
  43.             //string serverAppPath = Request.PhysicalApplicationPath.ToString();  
  44.             string serverAppPath = @”d:\”;  
  45.             //构成配置文件路径  
  46.             string con_file_path [email protected]””+ serverAppPath + @”config.json”;  
  47.   
  48.   
  49.             if (!File.Exists(con_file_path))  
  50.             {  
  51.                 File.Create(con_file_path);  
  52.             }  
  53.   
  54.         //把模型数据写到文件  
  55.             using (StreamWriter sw = new StreamWriter(con_file_path))  
  56.             {  
  57.                 try  
  58.                 {  
  59.   
  60.                     JsonSerializer serializer = new JsonSerializer();  
  61.                     serializer.Converters.Add(new JavaScriptDateTimeConverter());  
  62.                     serializer.NullValueHandling = NullValueHandling.Ignore;  
  63.   
  64.                     //创设Json.net的写入流  
  65.                     JsonWriter writer = new JsonTextWriter(sw);  
  66.                     //把模型数据系列化并写入Json.net的JsonWriter流中  
  67.                     serializer.Serialize(writer, cfm);  
  68.                     //ser.Serialize(writer, ht);  
  69.                     writer.Close();  
  70.                     sw.Close();  
  71.   
  72.                 }  
  73.   
  74.         }  
  75.             catch (Exception ex)  
  76.             {  
  77.                 ex.Message.ToString();  
  78.             }  
  79.   
  80.              //读取json文件  
  81.              using (StreamReader sr = new StreamReader(con_file_path))  
  82.              {  
  83.           try  
  84.                   {  
  85.                       JsonSerializer serializer = new JsonSerializer();  
  86.                       serializer.Converters.Add(new JavaScriptDateTimeConverter());  
  87.                       serializer.NullValueHandling = NullValueHandling.Ignore;  
  88.   
  89.                       //营造Json.net的读取流  
  90.                       JsonReader reader = new JsonTextReader(sr);  
  91.                       //对读取出的Json.net的reader流举办反系列化,并装载到模型中  
  92.                       cfm = serializer.Deserialize<ConfigFileModel>(reader);  
  93.   
  94.                       Response.Write(“<br/>”);  
  95.                       Response.Write(cfm.FileName+”, “+cfm.CreateDate);  
  96.           }  
  97.                   catch (Exception ex)  
  98.                   {  
  99.                       ex.Message.ToString();  
  100.                   }  
  101.               }  
  102.   
  103.         }  
  104.     }  
  105.   
  106.     public class ConfigFileModel  
  107.     {  
  108.         public ConfigFileModel() { }  
  109.   
  110.         string _fileName;  
  111.   
  112.         public string FileName  
  113.         {  
  114.             get { return _fileName; }  
  115.             set { _fileName = value; }  
  116.         }  
  117.         string _createDate;  
  118.   
  119.         public string CreateDate  
  120.         {  
  121.             get { return _createDate; }  
  122.             set { _createDate = value; }  
  123.         }  
  124.     }  
  125. }  

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq;
usingSystem.Web; usingSystem.Web.UI; usingSystem.Web.UI.WebCo…

//JSON标准类别化的声援类

public static string Serialize<T>(T data)
    {
        System.Runtime.Serialization.Json.DataContractJsonSerializer
serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
        using (MemoryStream ms = new MemoryStream())
        {
            serializer.WriteObject(ms, data);
            return Encoding.UTF8.GetString(ms.ToArray());
        }
    }

   //反系列化的扶持类

    public static T Deserialize<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        using (MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            System.Runtime.Serialization.Json.DataContractJsonSerializer
serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
            return (T)serializer.ReadObject(ms);
        }
    }

 

(c#)快速生成Json格式的数量:

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using System.IO; 

using Newtonsoft.Json; 

using System.Data; 

  

namespace Json.Common 

    /// <summary> 

    /// Json处理类 

    /// </summary> 

    public class JsonHelper 

    { 

        /// <summary> 

        /// 种类化数据为Json数据格式. 

        /// </summary> 

        /// <param name=”value”>The value.</param> 

        /// <returns></returns> 

        public static string Serialize(object value) 

        { 

            Type type = value.GetType(); 

  

            Newtonsoft.Json.JsonSerializer json = new
Newtonsoft.Json.JsonSerializer(); 

  

            json.NullValueHandling = NullValueHandling.Ignore; 

  

            json.ObjectCreationHandling =
Newtonsoft.Json.ObjectCreationHandling.Replace; 

            json.MissingMemberHandling =
Newtonsoft.Json.MissingMemberHandling.Ignore; 

            json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 

  

            if (type == typeof(DataRow)) 

                json.Converters.Add(new DataRowConverter()); 

            else if (type == typeof(DataTable)) 

                json.Converters.Add(new DataTableConverter()); 

            else if (type == typeof(DataSet)) 

                json.Converters.Add(new DataSetConverter()); 

  

            StringWriter sw = new StringWriter(); 

            Newtonsoft.Json.JsonTextWriter writer = new
JsonTextWriter(sw); 

           
//writer.Formatting = Formatting.Indented; 

            writer.Formatting = Formatting.None; 

  

            writer.QuoteChar = ‘”‘; 

            json.Serialize(writer, value); 

  

            string output = sw.ToString(); 

            writer.Close(); 

            sw.Close(); 

  

            return output; 

        } 

  

        /// <summary> 

        /// 反种类化Json数据格式. 

        /// </summary> 

        /// <param name=”jsonText”>The json text.</param> 

        /// <param name=”valueType”>Type of the
value.</param> 

        /// <returns></returns> 

        public static object Deserialize(string jsonText, Type
valueType) 

        { 

            Newtonsoft.Json.JsonSerializer json = new
Newtonsoft.Json.JsonSerializer(); 

  

            json.NullValueHandling =
Newtonsoft.Json.NullValueHandling.Ignore; 

            json.ObjectCreationHandling =
Newtonsoft.Json.ObjectCreationHandling.Replace; 

            json.MissingMemberHandling =
Newtonsoft.Json.MissingMemberHandling.Ignore; 

            json.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore; 

  

            StringReader sr = new StringReader(jsonText); 

            Newtonsoft.Json.JsonTextReader reader = new
JsonTextReader(sr); 

            object result = json.Deserialize(reader, valueType); 

            reader.Close(); 

  

            return result; 

        } 

  

        /// <summary> 

        /// 遍历DataTable的行和列生成Json,可控制性较差 

        /// </summary> 

        /// <param name=”dt”></param> 

        /// <returns></returns> 

        public static string DataTable2Json(DataTable dt) 

        { 

            StringBuilder jsonBuilder = new StringBuilder(); 

            jsonBuilder.Append(“{\””); 

            jsonBuilder.Append(dt.TableName); 

            jsonBuilder.Append(“\”:[“); 

            for (int i = 0; i < dt.Rows.Count; i++) 

            { 

                jsonBuilder.Append(“{“); 

                for (int j = 0; j < dt.Columns.Count; j++) 

                { 

                    jsonBuilder.Append(“\””); 

                    jsonBuilder.Append(dt.Columns[j].ColumnName); 

                    jsonBuilder.Append(“\”:\””); 

                    jsonBuilder.Append(dt.Rows[i][j].ToString()); 

                    jsonBuilder.Append(“\”,”); 

                } 

                jsonBuilder.Remove(jsonBuilder.Length – 1, 1); 

                jsonBuilder.Append(“},”); 

            } 

            jsonBuilder.Remove(jsonBuilder.Length – 1, 1); 

            jsonBuilder.Append(“]”); 

            jsonBuilder.Append(“}”); 

            return jsonBuilder.ToString(); 

        } 

  

        /// <summary> 

        ///
遍历DataTable的行和列生成Json,控制数据列,专门为FlexiGrid提供方便的多寡源生成 

        /// </summary> 

        /// <param name=”dt”>DataTable对象</param> 

        /// <param name=”cols”>Json中的数据列</param> 

        /// <returns></returns> 

        public static string Json4FlexiGrid(DataTable dt, string cols) 

        { 

            StringBuilder jsonBuilder = new StringBuilder(); 

            string[] colarr = cols.Split(‘,’); 

  

            if (dt.Rows.Count > 0) 

            { 

                for (int i = 0; i < dt.Rows.Count; i++) 

                { 

                    jsonBuilder.Append(“{“); 

                    jsonBuilder.Append(“\”id\”:”); 

                    jsonBuilder.Append(“\””); 

                   
jsonBuilder.Append(dt.Rows[i][“ID”].ToString()); 

                    jsonBuilder.Append(“\”,\”cell\”:[“); 

                    foreach (string col in colarr) 

                    { 

                        jsonBuilder.Append(“\””); 

                       
jsonBuilder.Append(dt.Rows[i][col].ToString()); 

                        jsonBuilder.Append(“\”,”); 

                    } 

                    jsonBuilder.Remove(jsonBuilder.Length – 1, 1); 

                    jsonBuilder.Append(“]},”); 

                } 

                jsonBuilder.Remove(jsonBuilder.Length – 1, 1); 

            } 

  

            return jsonBuilder.ToString(); 

        } 

    } 

  

JsonConverter  辅助类:

    /// <summary> 

    /// Converts a <see cref=”DataRow”/> object to and from JSON. 

    /// </summary> 

    public class DataRowConverter : JsonConverter 

    { 

        /// <summary> 

        /// Writes the JSON representation of the object. 

        /// </summary> 

        /// <param name=”writer”>The <see
cref=”JsonWriter”/> to write to.</param> 

        /// <param name=”dataRow”>The data row.</param> 

        /// <param name=”ser”>The JsonSerializer
对象.</param> 

        public override void WriteJson(JsonWriter writer, object
dataRow, JsonSerializer ser) 

        { 

            DataRow row = dataRow as DataRow; 

  

            writer.WriteStartObject(); 

            foreach (DataColumn column in row.Table.Columns) 

            { 

                writer.WritePropertyName(column.ColumnName); 

                ser.Serialize(writer, row[column]); 

            } 

            writer.WriteEndObject(); 

        } 

  

        /// <summary> 

        /// Determines whether this instance can convert the specified
value type. 

        /// </summary> 

        /// <param name=”valueType”>Type of the
value.</param> 

        /// <returns> 

        ///     <c>true</c> if this instance can convert the
specified value type; otherwise, <c>false</c>. 

        /// </returns> 

        public override bool CanConvert(Type valueType) 

        { 

            return typeof(DataRow).IsAssignableFrom(valueType); 

        } 

  

        /// <summary> 

        /// Reads the JSON representation of the object. 

        /// </summary> 

        /// <param name=”reader”>The <see
cref=”JsonReader”/> to read from.</param> 

        /// <param name=”objectType”>Type of the
object.</param> 

        /// <returns>The object value.</returns> 

        public override object ReadJson(JsonReader reader, Type
objectType, JsonSerializer ser) 

        { 

            throw new NotImplementedException(); 

        } 

    } 

  

    /// <summary> 

    /// Converts a DataTable to JSON. Note no support for
deserialization 

    /// </summary> 

    public class DataTableConverter : JsonConverter 

    { 

        /// <summary> 

        /// Writes the JSON representation of the object. 

        /// </summary> 

        /// <param name=”writer”>The <see
cref=”JsonWriter”/> to write to.</param> 

        /// <param name=”dataTable”>The data table.</param> 

        /// <param name=”ser”>The JsonSerializer
Object.</param> 

        public override void WriteJson(JsonWriter writer, object
dataTable, JsonSerializer ser) 

        { 

            DataTable table = dataTable as DataTable; 

            DataRowConverter converter = new DataRowConverter(); 

  

            writer.WriteStartObject(); 

  

            writer.WritePropertyName(“Rows”); 

            writer.WriteStartArray(); 

  

            foreach (DataRow row in table.Rows) 

            { 

                converter.WriteJson(writer, row, ser); 

            } 

  

            writer.WriteEndArray(); 

            writer.WriteEndObject(); 

        } 

  

        /// <summary> 

        /// Determines whether this instance can convert the specified
value type. 

        /// </summary> 

        /// <param name=”valueType”>Type of the
value.</param> 

        /// <returns> 

        ///     <c>true</c> if this instance can convert the
specified value type; otherwise, <c>false</c>. 

        /// </returns> 

        public override bool CanConvert(Type valueType) 

        { 

            return typeof(DataTable).IsAssignableFrom(valueType); 

        } 

  

        /// <summary> 

        /// Reads the JSON representation of the object. 

        /// </summary> 

        /// <param name=”reader”>The <see
cref=”JsonReader”/> to read from.</param> 

        /// <param name=”objectType”>Type of the
object.</param> 

        /// <returns>The object value.</returns> 

        public override object ReadJson(JsonReader reader, Type
objectType, JsonSerializer ser) 

        { 

            throw new NotImplementedException(); 

        } 

    } 

  

    /// <summary> 

    /// Converts a <see cref=”DataSet”/> object to JSON. No
support for reading. 

    /// </summary> 

    public class DataSetConverter : JsonConverter 

    { 

        /// <summary> 

        /// Writes the JSON representation of the object. 

        /// </summary> 

        /// <param name=”writer”>The <see
cref=”JsonWriter”/> to write to.</param> 

        /// <param name=”dataset”>The dataset.</param> 

        /// <param name=”ser”>The JsonSerializer
Object.</param> 

        public override void WriteJson(JsonWriter writer, object
dataset, JsonSerializer ser) 

        { 

            DataSet dataSet = dataset as DataSet; 

  

            DataTableConverter converter = new DataTableConverter(); 

  

            writer.WriteStartObject(); 

  

            writer.WritePropertyName(“Tables”); 

            writer.WriteStartArray(); 

  

            foreach (DataTable table in dataSet.Tables) 

            { 

                converter.WriteJson(writer, table, ser); 

            } 

            writer.WriteEndArray(); 

            writer.WriteEndObject(); 

        } 

  

        /// <summary> 

        /// Determines whether this instance can convert the specified
value type. 

        /// </summary> 

        /// <param name=”valueType”>Type of the
value.</param> 

        /// <returns> 

        ///     <c>true</c> if this instance can convert the
specified value type; otherwise, <c>false</c>. 

        /// </returns> 

        public override bool CanConvert(Type valueType) 

        { 

            return typeof(DataSet).IsAssignableFrom(valueType); 

        } 

  

        /// <summary> 

        /// Reads the JSON representation of the object. 

        /// </summary> 

        /// <param name=”reader”>The <see
cref=”JsonReader”/> to read from.</param> 

        /// <param name=”objectType”>Type of the
object.</param> 

        /// <returns>The object value.</returns> 

        public override object ReadJson(JsonReader reader, Type
objectType, JsonSerializer ser) 

        { 

            throw new NotImplementedException(); 

        } 

    } 

}

flexigrid是三个能够的javascript表格控件,通过Json四FlexiGrid方法,大家得以很有益的为flexigrid提供数据源。
JsonConverter来源于Newtonsoft.Json,那是2个Json的开源项目。

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有