在文件中读取、存储Json格式的字符串
public class Weather
{
static readonly string FilePath = System.Environment.CurrentDirectory + @"\Area.txt";
public static Models.Area GetCurrentArea()
{
var file = new FileInfo(FilePath);
Models.Area result;
if (!file.Exists)
{
//文件不存在就返回一个默认值,默认是成都
result = new Models.Area();
result.ID = 250;
result.Name = "成都";
result.ZoneID = 23;
//area.Zone = new Models.Zone() { ID = 23, Name = "四川" };
result.AreaCode = "56294";
}
else
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Models.Area));
using (var stream = file.Open(FileMode.Open, FileAccess.Read))
{
result = (Models.Area)ser.ReadObject(stream);
}
}
return result;
}
public static void SaveCurrentArea(Models.Area area)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Models.Area));
var file = new FileInfo(FilePath);
FileStream stream;
if (file.Exists)
{
stream = file.Open(FileMode.Truncate, FileAccess.Write);
}
else
{
stream = file.Open(FileMode.Create, FileAccess.Write);
}
using (stream)
{
ser.WriteObject(stream, area);
}
}
}
在文件中读取、存储Json格式的字符串的更多相关文章
- json中把非json格式的字符串转换成json对象再转换成json字符串
JSON.toJson(str).toString()假如key和value都是整数的时候,先转换成jsonObject对象,再转换成json字符串
- 一些常用的文本文件格式(TXT,JSON,CSV)以及如何从这些文件中读取和写入数据
TXT文件: txt是微软在操作系统上附带的一种文本格式,文件以.txt为后缀. 从txt文件中读取数据: with open ('xxx.txt') as file: data=file.readl ...
- Python3 将configparser从ini文件中读取的内容转换成字典格式
因为写脚本的用到了,所以研究了下怎么将configparser从ini文件中读取的内容转换成字典格式. 整理一下,希望能对大家有帮助. 从http://stackoverflow.com/questi ...
- 以ORM的思路来从Excel文件中读取JSON数据列表
1.一个常见的问题就是如何读取excel. 这里面有几个分支的问题,一个是如何使用poi读取excel,网上例子很多,但是这只解决了第一步.如何将excel读取入一定的数据结构这是第二个问题,还有就是 ...
- go从文件中读取json字符串并转换
go从文件中读取json字符串并转换 将要读取的文件的一部分 [ { "children": [ { "children": [ { "code&qu ...
- [转]C# JSON格式的字符串读取到类中
将JSON格式的字符串读取到类中 本例中建立JSON格式的字符串json,将其内容读取到Person类中 运行本代码需要添加引用动态库Newtonsoft.Json 程序代码: using Syste ...
- 条形码的应用三-----------从Excel文件中读取条形码
条形码的应用三------从Excel文件中读取条形码 介绍 上一篇文章,我向大家展示了生成多个条形码并存储到Excel文件中的一个方法.后来我又有了个想法:既然条码插入到excel中了,我可不可以从 ...
- Servlet从本地文件中读取图片,并显示在页面中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
- ios本地文件内容读取,.json .plist 文件读写
ios本地文件内容读取,.json .plist 文件读写 本地文件.json .plist文件是较为常用的存储本地数据的文件,对这些文件的操作也是一种常用的基础. 本文同时提供初始化变量的比较标准的 ...
随机推荐
- EXTJS 4.2 资料 控件之Window窗体添加html
//这里要跳转页面 var subWindow = new Ext.Window({ title: '窗口', width: width, height: height, modal: true,// ...
- Nhibernate 多对多级联删除
在网上找到的方法:查看这里 //-------------------------------------Article.hbm.xml-------------------------------- ...
- python 记录日志logging
在项目开发中,往往要记录日志文件.用python记录日志有两种方式: 1.利用python 自带的logging库,例如: # -*- coding: utf-8 -*- import osimpor ...
- [Windows Azure] Querying Tables and Entities
Constructing Filter Strings When constructing a filter string, keep these rules in mind: Use the log ...
- CQRS学习——Dpfb以及其他[引]
[Dpfb的起名源自:Ddd Project For Beginer,这个Beginer自然就是博主我自己了.请大家在知晓这是一个入门项目的事实上,怀着对入门者表示理解的心情阅读本系列.不胜感激.] ...
- C#读取网页源码
#region 1.读取 网页源码 + static string ReadHtml(string urlStr,int type) /// <summary> /// 读取 网页源码 + ...
- live555源码研究(三)------UsageEnvironment类
一.UsageEnvironment类作用 1,不使用的时候回收当前的使用环境. 2,对返回结果消息和错误消息的维护. 二.类UsageEnvironment继承关系图
- 210. Course Schedule II
题目: There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have ...
- Android TextView结合SpannableString使用
super.onCreate(savedInstanceState); TextView txtInfo = new TextView(this); SpannableString ss = new ...
- Complete The Pattern #6 - Odd Ladder
Complete The Pattern #6 - Odd Ladder Task: You have to write a function pattern which creates the fo ...