C#编程-101:读写Json文件示例_彭世瑜_新浪博客
【摘要】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Script.Serialization; using System.IO; using&...
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Script.Serialization;
- using System.IO;
- using Newtonsoft.Json;
- namespace JsonTest
- {
- class Person
- {
- public string name { get; set; }
- public int age { get; set; }
- public override string ToString()
- {
- return String.Format("Name : {0}\nAge : {1}",name,age);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //方法一:
- //Serialization class
- !important;">;
- //deserialize json from file
- string Jsonsting = File.ReadAllText("Json.json");
- Person p1 = ser.Deserialize(Jsonsting);
- Console.WriteLine(p1);
- //output json file
- Person p2 = new Person() {name="ben",age=23 };
- string outputJson = ser.Serialize(p2);
- File.WriteAllText("output.json",outputJson);
- //方法二:
- //Newtonsoft.Json --->Json.NET library
- //deserialize json from file
- string Jsonsting1 = File.ReadAllText("Json.json");
- Person p3 = JsonConvert.DeserializeObject(Jsonsting1);
- Console.WriteLine(p1);
- //output json file
- Person p4 = new Person() { name = "ben", age = 23 };
- string outputJson1 = JsonConvert.SerializeObject(p4);
- File.WriteAllText("output1.json", outputJson1);
- Console.ReadKey();
- }
- }
- }
备注:
(1)Serialization类需要引用: System.Web.Extensions
(2) Newtonsoft.Json类,需要在Nuget中安装json.net
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/109661838
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)