Json.net使用说明(01)
发布时间:2020-12-16 19:10:41 所属栏目:百科 来源:网络整理
导读:这两天在整理.net中各种类型与Json的转换方法,后有同事推荐了Json.net,于是试着研究了一下,并做了简单的测试。 总的来说,这是一个不错的辅助工具,这一节给出Json.net中常用类型序列化为Json字符串的用法,所有测试代码均为控制台应用程序。 1)序列化集
这两天在整理.net中各种类型与Json的转换方法,后有同事推荐了Json.net,于是试着研究了一下,并做了简单的测试。 总的来说,这是一个不错的辅助工具,这一节给出Json.net中常用类型序列化为Json字符串的用法,所有测试代码均为控制台应用程序。
1)序列化集合 <strong> </strong>/************* 序列化集合 **************/ static void Serialize_a_Collection() { //List赋值方法-01 List<string> videoGames = new List<string>(); videoGames.Add("Starcraft"); videoGames.Add("Halo"); videoGames.Add("Legend of Zelda"); //List赋值方法-02 //List<string> videoGames = new List<string> //{ // "Starcraft",// "Halo",// "Legend of Zelda" //}; //序列化 string json = JsonConvert.SerializeObject(videoGames); //序列化后的json数据格式 // ["Starcraft","Halo","Legend of Zelda"] Console.WriteLine(json); }
执行结果:
2)序列化对象 <span style="font-weight: bold;"> </span>/************* 序列化对象 **************/ static void Serialize_an_Object() { Account account = new Account { Email = "james@example.com",Active = true,CreatedDate = new DateTime(2013,1,20,DateTimeKind.Utc),Roles = new List<string> { "User","Admin" } }; //序列化 string json = JsonConvert.SerializeObject(account,Formatting.Indented); // 序列化后的json数据格式: // { // "Email": "james@example.com",// "Active": true,// "CreatedDate": "2013-01-20T00:00:00Z",// "Roles": [ // "User",// "Admin" // ] // } //string json = JsonConvert.SerializeObject(account); // 序列化后的json数据格式: Console.WriteLine(json); } <strong> </strong>public class Account { public string Email { get; set; } public bool Active { get; set; } public DateTime CreatedDate { get; set; } public IList<string> Roles { get; set; } }
执行结果
3)序列化字典
<span style="font-weight: bold;"> </span>/************* 序列化字典 **************/ static void Serialize_a_Dictionary() { // Dictionary赋值方式-01 Dictionary<string,int> points = new Dictionary<string,int> { { "James",9001 },{ "Jo",3474 },{ "Jess",11926 } }; // Dictionary赋值方式-02 //Dictionary<string,int>(); //points.Add("James",9001); //points.Add("Jo",3474); //points.Add("Jess",11926); //序列化 string json = JsonConvert.SerializeObject(points,Formatting.Indented); // 序列化后的json数据格式: // { // "James": 9001,// "Jo": 3474,// "Jess": 11926 // } Console.WriteLine(json); }/* * PS: Dictionary用法介绍 * 1)http://www.cnblogs.com/linzheng/archive/2010/12/13/1904709.html * 2)http://www.cnblogs.com/linlf03/archive/2011/12/09/2282574.html */
执行结果
4)序列化Json到文件
<span style="font-weight: bold;"> </span>/************* 序列化Json到文件 **************/ static void Serialize_JSON_to_a_file() { // 类的赋值方式-01 //Movie movie = new Movie(); //movie.Name = "Bad Boys"; //movie.Year = 1995; // 类的赋值方式-02 Movie movie = new Movie { Name = "Bad Boys",Year = 1995 }; // serialize JSON to a string and then write string to a file File.WriteAllText(@"c:movie.json",JsonConvert.SerializeObject(movie)); // serialize JSON directly to a file using (StreamWriter file = File.CreateText(@"c:movie.json")) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file,movie); } } public class Movie { public string Name { get; set; } public int Year { get; set; } } /* * PS:using()用法介绍 * using语句,定义一个范围,在范围结束时处理对象。 * 场景: * 当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。 * 要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。 * 例如: * using (Class1 cls1 = new Class1(),cls2 = new Class1()) * { * // the code using cls1,cls2 * } // call the Dispose on cls1 and cls2 */
5)序列化DataSet
<strong> </strong>/************* 序列化DataSet **************/ static void Serialize_a_DataSet() { // 手动创建DataSet DataSet dataSet = new DataSet("dataSet"); dataSet.Namespace = "NetFrameWork"; DataTable table = new DataTable(); DataColumn idColumn = new DataColumn("id",typeof(int)); idColumn.AutoIncrement = true; DataColumn itemColumn = new DataColumn("item"); table.Columns.Add(idColumn); table.Columns.Add(itemColumn); dataSet.Tables.Add(table); for (int i = 0; i < 2; i++) { DataRow newRow = table.NewRow(); newRow["item"] = "item " + i; table.Rows.Add(newRow); } dataSet.AcceptChanges(); //序列化 string json = JsonConvert.SerializeObject(dataSet,Formatting.Indented); // 序列化后的json数据格式: // { // "Table1": [ // { // "id": 0,// "item": "item 0" // },// { // "id": 1,// "item": "item 1" // } // ] // } Console.WriteLine(json); } }
执行结果
以上对常用的数据类型转换为Json的使用方法做了简单的测试,需要说明的是,json.net的功能非常完善,除了以上的常用数据类型外,还有许多的使用情况,若有需要,后期继续补充。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |