添加JSON Data到已经存在的JSON文件中
早上在学习《Post model至Web Api创建或是保存数据》http://www.cnblogs.com/insus/p/4343833.html ,如果你第二添加时,json文件得到的数据只能是单笔记录且是最新的。
那需要怎样把新添加的json数据附加至已经存在的数据中去?本篇Insus.NET就是想实现此功能。
想法是先读取json文件的数据转换为数据集存放在内存中,新添加的数据再附加上去,然后再把内存的数据集序列化保存为json文件即可。
上面代码示例中,有3大部分,第一部分是读取文件中原有数据:
if (System.IO.File.Exists(existFilePhysicalPath))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(existFilePhysicalPath))
{
JsonTextReader jtr = new JsonTextReader(sr);
JsonSerializer se = new JsonSerializer();
object obj = se.Deserialize(jtr, typeof(List<Order>));
orders = (List<Order>)obj;
}
}
其实这部分,简单一句代码也可以:
orders = JsonConvert.DeserializeObject<List<Order>>(System.IO.File.ReadAllText(existFilePhysicalPath));
第二部分是将内存中的List<Order>数据序列化之后,存为json文件:
using (FileStream fs = File.Open(newFilePhysicalPath, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, orders);
}
第三部分是把新创建的文件重命名为旧文件名:
if (System.IO.File.Exists(existFilePhysicalPath))
{
File.Delete(existFilePhysicalPath);
}
if (System.IO.File.Exists(newFilePhysicalPath))
{
System.IO.File.Move(newFilePhysicalPath, existFilePhysicalPath);
}
在Orders目录中,新创建一个html网页SaveJsonToExistFile.html:
上面收集合的jQuery代码,可以参考下面:
Insus.NET所做的练习,一般最后少不了动画演示:

添加JSON Data到已经存在的JSON文件中的更多相关文章
- Guzzle Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
项目更新到正式平台时,出现Guzzle(5.3) client get请求出现:Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, ...
- 【转】如何把Json格式字符写进text文件中
http://www.cnblogs.com/insus/p/4306640.html http://json2csharp.chahuo.com/ 本篇一步一步学习怎样把显示于网页的json格式的字 ...
- 如何把Json格式字符写进text文件中
本篇一步一步学习怎样把显示于网页的json格式的字符串写进text文件中,并保存起来.学习到创建model, Entity, 序列化List<object>转换为json,显示于网页上.然 ...
- directly receive json data from javascript in mvc
if you send json data to mvc,how can you receive them and parse them more simply? you can do it like ...
- SQL to JSON Data Modeling with Hackolade
Review: SQL to JSON data modeling First, let’s review, the main way to represent relations in a rela ...
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 co ...
- PHP convet class to json data
/********************************************************************* * PHP convet class to json da ...
- json data 解析demo
json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...
随机推荐
- java文件复制函数
在写代码中,经常会需要复制文件.这时候就可以把这些函数放到工具类里面. 下面是一个复制文件的函数: public class CopyFileUtil { private static String ...
- C#复习⑤
C#复习⑤ 2016年6月19日 22:39 Main Inheritance 继承 1.继承的语法结构 class A { // base class int a; public A() {...} ...
- 1.7 基础知识——GP2.6 配置管理(CM)
摘要: 某某工作产品要进行配置管理,并不意味非要进行很重型的配置管理,根据实际需要采取合适的方式就可以了. 正文: GP2.6 Place designed work products of XXX ...
- 在ROS中使用Python3
Use Python3 in ROS. 以下内容在Ubuntu 16.04 x64和ROS kinetic中测试通过 事实上,只要在.py文件加上python3的shebang,rosrun的时候就会 ...
- JavaWeb防止表单重复提交(转载)
转载自:http://blog.csdn.net/ye1992/article/details/42873219 在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用 ...
- 【hadoop】——window下elicpse连接hadoop集群基础超详细版
1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...
- PHP MSSQL 分页实例(刷新)
<?php/* '页面说明:*/ $link=mssql_connect("MYSQL2005","sa","123456") or ...
- top:failed tty get 错误
运行命令,ps -ef | grep test | grep -v test | awk '{ print $2 }' | xargs top -H -p 想看test的实时状态,结果报了错,查了一下 ...
- Windows Azure 虚拟机的IP地址操作
Windows Azure上的一个虚拟机对应两个IP地址,VIP和DIP. VIP,公网IPv4地址,动态分配.虚拟机停止(deallocate,在管理控制台上关机或者使用PowerShell关机)后 ...
- 扩展欧几里德算法 cogs.tk 2057. [ZLXOI2015]殉国
2057. [ZLXOI2015]殉国 ★☆ 输入文件:BlackHawk.in 输出文件:BlackHawk.out 评测插件时间限制:0.05 s 内存限制:256 MB [题目描 ...