- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Text;
- using Newtonsoft.Json;
- using com.zjpx.model;
- using System.Collections;
- using Newtonsoft.Json.Converters;
- namespace web
- {
- public partial class testJson : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ConfigFileModel cfm = new ConfigFileModel(); //实体模型类
- cfm.CreateDate = "2012-02-23";
- cfm.FileName = "test.txt";
- //可以操作集合
- Hashtable ht = new Hashtable();
- ht.Add("hs_name", "alex");
- ht.Add("hs_pwd", "ggg");
- //序列化
- string js1 = JsonConvert.SerializeObject(cfm);
- string js2 = JsonConvert.SerializeObject(ht);
- Response.Write(js1);
- Response.Write("<br/>");
- Response.Write(js2);
- //"{\"FileName\":\"test.txt\",\"CreateDate\":\"2012-02-23\"}"
- //"{\"hs_pwd\":\"ggg\",\"hs_name\":\"alex\"}"
- //反序列化
- ConfigFileModel debc1 = JsonConvert.DeserializeObject<ConfigFileModel>(js1);
- ConfigFileModel debc2 = JsonConvert.DeserializeObject<ConfigFileModel>(js2);
- //找到服务器物理路径
- //string serverAppPath = Request.PhysicalApplicationPath.ToString();
- string serverAppPath = @"d:\";
- //构成配置文件路径
- string con_file_path =@""+ serverAppPath + @"config.json";
- if (!File.Exists(con_file_path))
- {
- File.Create(con_file_path);
- }
- //把模型数据写到文件
- using (StreamWriter sw = new StreamWriter(con_file_path))
- {
- try
- {
- JsonSerializer serializer = new JsonSerializer();
- serializer.Converters.Add(new JavaScriptDateTimeConverter());
- serializer.NullValueHandling = NullValueHandling.Ignore;
- //构建Json.net的写入流
- JsonWriter writer = new JsonTextWriter(sw);
- //把模型数据序列化并写入Json.net的JsonWriter流中
- serializer.Serialize(writer, cfm);
- //ser.Serialize(writer, ht);
- writer.Close();
- sw.Close();
- }
- }
- catch (Exception ex)
- {
- ex.Message.ToString();
- }
- //读取json文件
- using (StreamReader sr = new StreamReader(con_file_path))
- {
- try
- {
- JsonSerializer serializer = new JsonSerializer();
- serializer.Converters.Add(new JavaScriptDateTimeConverter());
- serializer.NullValueHandling = NullValueHandling.Ignore;
- //构建Json.net的读取流
- JsonReader reader = new JsonTextReader(sr);
- //对读取出的Json.net的reader流进行反序列化,并装载到模型中
- cfm = serializer.Deserialize<ConfigFileModel>(reader);
- Response.Write("<br/>");
- Response.Write(cfm.FileName+", "+cfm.CreateDate);
- }
- catch (Exception ex)
- {
- ex.Message.ToString();
- }
- }
- }
- }
- public class ConfigFileModel
- {
- public ConfigFileModel() { }
- string _fileName;
- public string FileName
- {
- get { return _fileName; }
- set { _fileName = value; }
- }
- string _createDate;
- public string CreateDate
- {
- get { return _createDate; }
- set { _createDate = value; }
- }
- }
- }
相关文章
- C语言:用字符读取流和输出流来读写入数据。(文本文件)
- angular2^ typescript 将 文件和Json数据 合并发送到服务器(1.客户端处理)
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律
- ArcGIS Pro SDK 002 对数据文件的读取和操作
- Qt编写视频监控系统70-0SD标签和图形信息(支持写入到文件)
- IOSerialize,xml和json,soap序列化器,二进制序列化器,XML序列化器,文件 检查、新增、复制、移动、删除
- 将CStdioFile类扩展,读取UNICODE和ANSI文本文件
- 使用OLEDB读取excel和csv文件
- 如何获取c#中解析器动态生成的.json文件的键和值