你的位置:首页 > ASP.net教程

[ASP.net教程]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 =@""+ 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. }