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

网友投稿 279 2022-08-22

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

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

是NET的流行高性能JSON框架

特性

工具

VS2010+

Newtonsoft库

从NuGet下载合适的Newtonsoft.Json库

Dotnet讲堂

这里是Dotnet编程的乐园,如果你对C# Winfom感兴趣,你想学习wpf编写酷炫的界面通过mvvm模式实现前后端解耦,你热衷于智能制造、工控上位机,资深高工带你入坑。

示例

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

public class Account{ public string Email { get; set; } public bool Active { get; set; } public DateTime CreatedDate { get; set; } public IList Roles { get; set; }}

Account account = new Account{ Email = "james@example.com", Active = true, CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc), Roles = new List { "User", "Admin" }};string json = JsonConvert.SerializeObject(account, Formatting.Indented);// {// "Email": "james@example.com",// "Active": true,// "CreatedDate": "2013-01-20T00:00:00Z",// "Roles": [// "User",// "Admin"// ]// }Console.WriteLine(json);

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

JArray array = new JArray();array.Add("Manual text");array.Add(new DateTime(2000, 5, 23));JObject o = new JObject();o["MyArray"] = array;string json = o.ToString();// {// "MyArray": [// "Manual text",// "2000-05-23T00:00:00"// ]// }

3、JSON模式-加载模式并验证JSON。请注意,JSON Schema验证已移至其自己的程序包。有关 更多详细信息,请参见o = JObject.Parse(@"{ 'Stores': [ 'Lambton Quay', 'Willis Street' ], 'Manufacturers': [ { 'Name': 'Acme Co', 'Products': [ { 'Name': 'Anvil', 'Price': 50 } ] }, { 'Name': 'Contoso', 'Products': [ { 'Name': 'Elbow Grease', 'Price': 99.95 }, { 'Name': 'Headlight Fluid', 'Price': 4 } ] } ]}");string name = (string)o.SelectToken("Manufacturers[0].Name");Console.WriteLine(name);// Acme Codecimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");Console.WriteLine(productPrice);// 50string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");Console.WriteLine(productName);// Elbow Grease

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

string json = @"{ '@Id': 1, 'Email': 'james@example.com', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z', 'Roles': [ 'User', 'Admin' ], 'Team': { '@Id': 2, 'Name': 'Software Developers', 'Description': 'Creators of fine software products and services.' }}";XNode node = JsonConvert.DeserializeXNode(json, "Root");Console.WriteLine(node.ToString());// // james@example.com// true// 2013-01-20T00:00:00Z// User// Admin// // Software Developers// Creators of fine software products and services.// //

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

public class Event{ public string Name { get; set; } public DateTime StartDate { get; set; }}

Event e = new Event{ Name = "Movie Premiere", StartDate = new DateTime(2013, 1, 22, 20, 30, 0, DateTimeKind.Utc)};MemoryStream ms = new MemoryStream();using (BsonWriter writer = new BsonWriter(ms)){ JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, e);}string data = Convert.ToBase64String(ms.ToArray());Console.WriteLine(data);// MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAED982M8AQAAAA==

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

string json = @"{ 'CPU': 'Intel', 'PSU': '500W', 'Drives': [ 'DVD read/writer' /*(broken)*/, '500 gigabyte hard drive', '200 gigabyte hard drive' ]}";JsonTextReader reader = new JsonTextReader(new StringReader(json));while (reader.Read()){ if (reader.Value != null) { Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value); } else { Console.WriteLine("Token: {0}", reader.TokenType); }}// Token: StartObject// Token: PropertyName, Value: CPU// Token: String, Value: Intel// Token: PropertyName, Value: PSU// Token: String, Value: 500W// Token: PropertyName, Value: Drives// Token: StartArray// Token: String, Value: DVD read/writer// Token: Comment, Value: (broken)// Token: String, Value: 500 gigabyte hard drive// Token: String, Value: 200 gigabyte hard drive// Token: EndArray// Token: EndObject

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PerfView专题 (第一篇): 如何寻找热点函数
下一篇:Python中有用的字符串方法(python字符串操作符有哪些)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~