NET流行高性能JSON框架之Json.NET

开发 后端
在日常编程中经常会使用到Json来进行数据的交互好在.Net平台下有很多开源的Json库使得我们能够比较轻松快速的处理各种复杂的Json,其中Newtonsoft库。

[[416726]]

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。

在日常编程中经常会使用到Json来进行数据的交互好在.Net平台下有很多开源的Json库使得我们能够比较轻松快速的处理各种复杂的Json,其中Newtonsoft库

是NET的流行高性能JSON框架

特性

工具

VS2010+

Newtonsoft库

从NuGet下载合适的Newtonsoft.Json库

1.在你需要引用Newtosoft.Json的项目上,点击鼠标右键,管理Nuget程序包即可打开项目包管理器。

2.在包管理器中输入关键字"Json"看到Newtosoft.Json这个库 点击右下加的箭头即可完成安装。

示例

1、序列化JSON-序列化和反序列化JSON,序列化程序设置和序列化属性

  1. public class Account 
  2.     public string Email { get; set; } 
  3.     public bool Active { get; set; } 
  4.     public DateTime CreatedDate { get; set; } 
  5.     public IList<string> Roles { get; set; } 
  1. Account account = new Account 
  2.     Email = "james@example.com"
  3.     Active = true
  4.     CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc), 
  5.     Roles = new List<string> 
  6.     { 
  7.         "User"
  8.         "Admin" 
  9.     } 
  10. }; 
  11.  
  12. string json = JsonConvert.SerializeObject(account, Formatting.Indented); 
  13. // { 
  14. //   "Email""james@example.com"
  15. //   "Active"true
  16. //   "CreatedDate""2013-01-20T00:00:00Z"
  17. //   "Roles": [ 
  18. //     "User"
  19. //     "Admin" 
  20. //   ] 
  21. // } 
  22.  
  23. Console.WriteLine(json); 

2、LINQ to JSON-解析,查询,修改和编写JSON

  1. JArray array = new JArray(); 
  2. array.Add("Manual text"); 
  3. array.Add(new DateTime(2000, 5, 23)); 
  4.  
  5. JObject o = new JObject(); 
  6. o["MyArray"] = array; 
  7.  
  8. string json = o.ToString(); 
  9. // { 
  10. //   "MyArray": [ 
  11. //     "Manual text"
  12. //     "2000-05-23T00:00:00" 
  13. //   ] 
  14. // } 

3、JSON模式-加载模式并验证JSON。请注意,JSON Schema验证已移至其自己的程序包。有关 更多详细信息,请参见https://www.newtonsoft.com/jsonschema。

  1. JObject o = JObject.Parse(@"{ 
  2.   'Stores': [ 
  3.     'Lambton Quay'
  4.     'Willis Street' 
  5.   ], 
  6.   'Manufacturers': [ 
  7.     { 
  8.       'Name''Acme Co'
  9.       'Products': [ 
  10.         { 
  11.           'Name''Anvil'
  12.           'Price': 50 
  13.         } 
  14.       ] 
  15.     }, 
  16.     { 
  17.       'Name''Contoso'
  18.       'Products': [ 
  19.         { 
  20.           'Name''Elbow Grease'
  21.           'Price': 99.95 
  22.         }, 
  23.         { 
  24.           'Name''Headlight Fluid'
  25.           'Price': 4 
  26.         } 
  27.       ] 
  28.     } 
  29.   ] 
  30. }"); 
  31.  
  32. string name = (string)o.SelectToken("Manufacturers[0].Name"); 
  33.  
  34. Console.WriteLine(name); 
  35. // Acme Co 
  36.  
  37. decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price"); 
  38.  
  39. Console.WriteLine(productPrice); 
  40. // 50 
  41.  
  42. string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name"); 
  43.  
  44. Console.WriteLine(productName); 
  45. // Elbow Grease 

4、转换XML-将JSON转换为XML和XML转换为JSON

  1. string json = @"{ 
  2.   '@Id': 1, 
  3.   'Email''james@example.com'
  4.   'Active'true
  5.   'CreatedDate''2013-01-20T00:00:00Z'
  6.   'Roles': [ 
  7.     'User'
  8.     'Admin' 
  9.   ], 
  10.   'Team': { 
  11.     '@Id': 2, 
  12.     'Name''Software Developers'
  13.     'Description''Creators of fine software products and services.' 
  14.   } 
  15. }"; 
  16.  
  17. XNode node = JsonConvert.DeserializeXNode(json, "Root"); 
  18.  
  19. Console.WriteLine(node.ToString()); 
  20. // <Root Id="1"
  21. //   <Email>james@example.com</Email> 
  22. //   <Active>true</Active> 
  23. //   <CreatedDate>2013-01-20T00:00:00Z</CreatedDate> 
  24. //   <Roles>User</Roles> 
  25. //   <Roles>Admin</Roles> 
  26. //   <Team Id="2"
  27. //     <Name>Software Developers</Name
  28. //     <Description>Creators of fine software products and services.</Description> 
  29. //   </Team> 
  30. // </Root> 

5、BSON-序列化和反序列化BSON

  1. public class Event 
  2.     public string Name { get; set; } 
  3.     public DateTime StartDate { get; set; } 
  1. Event e = new Event 
  2.     Name = "Movie Premiere"
  3.     StartDate = new DateTime(2013, 1, 22, 20, 30, 0, DateTimeKind.Utc) 
  4. }; 
  5.  
  6. MemoryStream ms = new MemoryStream(); 
  7. using (BsonWriter writer = new BsonWriter(ms)) 
  8.     JsonSerializer serializer = new JsonSerializer(); 
  9.     serializer.Serialize(writer, e); 
  10.  
  11. string data = Convert.ToBase64String(ms.ToArray()); 
  12.  
  13. Console.WriteLine(data); 
  14. // MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAED982M8AQAAAA== 

6、读取和写入JSON-使用JsonTextReader读取JSON,使用JsonTextWriter写入JSON

  1. string json = @"{ 
  2.    'CPU''Intel'
  3.    'PSU''500W'
  4.    'Drives': [ 
  5.      'DVD read/writer' 
  6.      /*(broken)*/, 
  7.      '500 gigabyte hard drive'
  8.      '200 gigabyte hard drive' 
  9.    ] 
  10. }"; 
  11.  
  12. JsonTextReader reader = new JsonTextReader(new StringReader(json)); 
  13. while (reader.Read()) 
  14.     if (reader.Value != null
  15.     { 
  16.         Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value); 
  17.     } 
  18.     else 
  19.     { 
  20.         Console.WriteLine("Token: {0}", reader.TokenType); 
  21.     } 
  22.  
  23. // Token: StartObject 
  24. // Token: PropertyName, Value: CPU 
  25. // Token: String, Value: Intel 
  26. // Token: PropertyName, Value: PSU 
  27. // Token: String, Value: 500W 
  28. // Token: PropertyName, Value: Drives 
  29. // Token: StartArray 
  30. // Token: String, Value: DVD read/writer 
  31. // Token: Comment, Value: (broken) 
  32. // Token: String, Value: 500 gigabyte hard drive 
  33. // Token: String, Value: 200 gigabyte hard drive 
  34. // Token: EndArray 
  35. // Token: EndObject 

更多功能见https://github.com/JamesNK/Newtonsoft.Json

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2010-01-05 14:01:27

JSON.NET

2009-08-18 17:39:12

JSON.NET

2011-02-13 09:17:02

ASP.NET

2011-02-23 09:49:40

ASP.NET

2011-02-15 09:31:56

ASP.NET

2011-02-16 09:08:27

ASP.NET

2024-01-30 13:32:51

JSON反序列化序列化

2011-02-22 09:16:24

高性能ASP.NET

2011-02-13 09:37:55

ASP.NET

2011-02-17 09:13:57

ASP.NET

2011-02-14 09:32:16

ASP.NET

2022-06-29 08:55:46

orjsonPythonJSON

2024-04-12 12:14:07

C#接口开发

2023-10-31 18:52:29

网络框架XDP技术

2010-07-22 09:13:00

ASP.NET

2011-04-22 16:23:16

ASP.NET动态应用系统

2023-01-11 15:17:01

gRPC.NET 7

2010-07-01 09:25:18

Lift 2.0Web应用框架Scala Lift

2023-06-19 07:54:37

DotNetty网络通信框架

2016-05-20 14:20:31

ASP.NET建议
点赞
收藏

51CTO技术栈公众号