Json.NET读写Json文件(转)
来源:互联网 发布:网络推广思维导图 编辑:程序博客网 时间:2024/05/20 13:40
Json文件的写入读取
采用的Json.NET提供的API。
http://james.newtonking.com/projects/json-net.aspx
http://www.codeplex.com/json/
以下是demo代码
- 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; }
- }
- }
- }
0 0
- Json.NET读写Json文件(转)
- Json.NET读写Json文件
- php读写json文件
- nodejs读写json文件
- json-c 读写文件
- cocos2dx读写json文件
- Python读写json文件
- python读写json文件
- Python读写json文件
- Python读写json文件
- Python 读写json文件
- python 读写json文件
- Unity3d Json 读写文件
- python读写json文件
- json文件的解析(读写操作)
- 用.net dynamic实现对JSON文件的读写操作
- php 读写json文件。追加,修改json
- 关于Json文件的读写
- Shiro源码分析-初始化-SecurityManager
- 数据库序列( SEQUENCE)的使用
- 广电总局发布TVOS2.0,首批63家成员单位(含乐视致新)
- for和forin区别
- vm硬盘及主板
- Json.NET读写Json文件(转)
- qt控件的使用
- 如何知道我在通知栏的展示的 Notification 被点击了?
- Alex 的 Hadoop 菜鸟教程: 第12课 Sqoop1 安装/导入/导出教程
- 类与类的四种关系
- arm qt-4.5.0更换为qt4.8.5
- Android细节:OOM,图片,layout
- Reactive-Native学习:iOS工程内嵌React-Native(一)
- angularjsjs require : 'ngModel'