早上在学习《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文件中的更多相关文章

  1. 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, ...

  2. 【转】如何把Json格式字符写进text文件中

    http://www.cnblogs.com/insus/p/4306640.html http://json2csharp.chahuo.com/ 本篇一步一步学习怎样把显示于网页的json格式的字 ...

  3. 如何把Json格式字符写进text文件中

    本篇一步一步学习怎样把显示于网页的json格式的字符串写进text文件中,并保存起来.学习到创建model, Entity, 序列化List<object>转换为json,显示于网页上.然 ...

  4. 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 ...

  5. 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 ...

  6. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决

    记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...

  7. 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 ...

  8. PHP convet class to json data

    /********************************************************************* * PHP convet class to json da ...

  9. json data 解析demo

    json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...

随机推荐

  1. NSArray Sort 排序

    打算实现tableview的生序降序排序 ```js NSArray * rs= [oneArray sortedArrayUsingComparator:NSComparisonResult(RFI ...

  2. 软工_Alpha阶段事后分析总计

    1.设想和目标 1.1 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要解决狼人杀玩家在游戏时的一些痛点.因为之前自己对于游戏中那些不方便的地方有过体 ...

  3. winform ListView应用之分组、重绘图标、网格线 (c# .net winform)

    最近在winform应用中需要用到可分组的数据列表功能,DataGridView默认没有提供分组的功能,而OutlookGrid(http://www.codeproject.com/KB/grid/ ...

  4. 编写一个Java应用程序,该应用程序包括2个类:Print类和主类E。Print 类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的 立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类E的main方法中来 测试类Print

    package zuoye; public class print { void output() { System.out.println("100-999之间的水仙花数是:") ...

  5. 0003 64位Oracle11gR2不能运行SQL Developer的解决方法

    "应用程序开发"下的"SQL Developer"双击不可用,出现“Windows正在查找SQLDEVELOPER.BAT"的提示,如下图: 搜索博客 ...

  6. C#设计模式(21)——责任链模式

    一.引言 在现实生活中,有很多请求并不是一个人说了就算的,例如面试时的工资,低于1万的薪水可能技术经理就可以决定了,但是1万~1万5的薪水可能技术经理就没这个权利批准,可能就需要请求技术总监的批准,所 ...

  7. 如何编译ReactNative示例程序Examples

    通过示例程序可以看到一些基本组件的使用,对于学习ReactNative是很有帮助的. 编译示例程序需要将整个项目导入到androidStudio中,androidStudio导入项目时选择react- ...

  8. 烂泥:php5.6源码安装及php-fpm配置与nginx集成

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. LNMP环境的搭建中,现在只有php没有源码安装过.这篇文章就把这个介绍下. 注意本篇文章使用的centos 6.5 64bit. 登陆centos下载 ...

  9. Java Hello World例子和添加按钮事件与功能

    新建android工程,然后默认“下一步”即可完成创建: 2.添加Button 3.在src的MainActivity.java添加以下红色代码 import android.support.v7.a ...

  10. [转]ASP.NET MVC 3 Razor + jqGrid 示例

    本文转自:http://www.cnblogs.com/think8848/archive/2011/07/15/2107828.html 前些天写了一篇有关jqGrid的文章,因为是公司项目的原因一 ...