Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Json.Net6.0

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Json.Net6.0". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Json.Net6.0.

Preface

JSON (JavaScript Object Notation) is a lightweight data exchange format. Simply put, JSON can convert a set of data represented in a JavaScript object into a string, which can then be easily passed between functions, or from a Web client to a server-side program in an asynchronous application. This string looks a bit odd, but JavaScript can easily interpret it, and JSON can represent a more complex structure than a "name / value pair." For example, you can represent arrays and complex objects, not just a simple list of keys and values.

It has the following characteristics:

1. Flexible JSON serializer for converting between .NET objects and JSON-flexible JSON serializer is used to convert .NET objects to JSON.

2. LINQ to JSON for manually reading and writing JSON-- LINQ to JSON is used to read and write Json manually

3. High performance, faster than. NET's built-in JSON serializers-- has high performance and is faster than the built-in JSON serializer in .NET.

4. Writes indented, easy to read JSON-indented writing to make Json easy to read

5. Convert JSON to and from XML-- supports the conversion between JSON and XML.

6. Supports .NET 2.NET3.5.NET4 Legend Silverlight Phone and Windows 8 hands-support .NET 2.NET3.5.NET4, Silverlight,Windows Phone and Windows 8

The objects that have read and write Json built into .net are DataContractJsonSerializer and JavaScriptSerializer. But this is only supported after .net 3.5. And the performance is not excellent.

The performance comparison chart is as follows:

This is a performance comparison between Json.Net 5 and .NET built-in objects, and now that the version of Json.Net has reached Version 6.0.1, it is believed that it will perform better. JSON.NET is open source, download address: http://json.codeplex.com/, here is the complete source code, of course, you can also refer to http://james.newtonking.com/ here.

Jb51 download address: https://www.yisu.com/codes/571698.html

Of course, if you enter the Nuget management package through VS, you can also download Json.Net packages.

Let's take a preliminary look at JSON.NET through an example.

Serialize an object to a json string.

2. Then deserialize the string into an object.

3. The interchange between JSON and XML.

4. Of course, there are other format conversions, we will not give examples for the time being, which will be described in more detail in the following article.

Serialize an Object-- serializes an object

First define an entity class object

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

Instantiate the entity class and then serialize it into a Json string

Account account = new Account () {Email = "aehyok@vip.qq.com", Active = true, CreatedDate = new DateTime (2014, 3, 27, 0, 0, 0, DateTimeKind.Utc), Roles = new List {"aehyok", "Kris"}}; string json = JsonConvert.SerializeObject (account, Formatting.Indented)

The result of the json string is

{"Email": "aehyok@vip.qq.com", "Active": true, "CreatedDate": "2014-03-27T00:00:00Z", "Roles": ["aehyok", "Kris"]} Deserialize an Object-- deserializes an object

Continue to use the entity class above

Now it's time to define a Json string, or we can simply modify the Json string generated above and then deserialize it.

String json = @ "{'Email':' aehyok@vip.qq.com', 'Active': true,' CreatedDate': '2014-03-27T00VOV 00Zhe,' Roles': ['aehyok',' Kris']}" Account account = JsonConvert.DeserializeObject (json)

The data obtained through debugging is

Convert JSON to XML-- converts JSON to XML

First define a Json string and then convert it

String json = @ "{'@ Id': 1, 'Email':' aehyok@viq.qq.com', 'Active': true,' CreatedDate': '2014-01-20T00 Email': 0000Zhe,' Roles': ['Kris',' aehyok'] 'Team': {' @ Id': 2, 'Name':' Software Developers', 'Description':' Creators of fine software products and services.'} " XNode node = JsonConvert.DeserializeXNode (json, "Root")

The conversion result is

Convert XML to JSON-- converts XML to JSON

Now you just need to define a simple XML string and then convert it.

String xml = @ "aehyok http://www.google.com Kris http://www.baidu.com"; XmlDocument doc = new XmlDocument (); doc.LoadXml (xml) String json = JsonConvert.SerializeXmlNode (doc)

The result obtained by debugging is

Introduction to other Featur

1. Linq to JSON is used to manipulate JSON objects. Can be used to quickly query, modify, delete, and create JSON objects. When the JSON object content is complex and we only need a small portion of the data, we can consider using Linq to JSON to read and modify part of the data instead of deserializing all of it.

2. Json.Net not only supports serialization and deserialization of objects, but also supports conversion between XML and JSON. It supports conversion between data types and JSON, such as DataSet, DataTable, Entity Framework and NHibernate, Collection, Dictionary, Dynamic and so on. Most data types basically support it.

3. JSON.NET supports serialization and deserialization of BSON data.

BSON (Binary Serialized Document Format) is a binary storage format similar to json, referred to as Binary JSON. Like JSON, it supports embedded document objects and array objects, but BSON has some data types that JSON does not have, such as Date and BinData types.

4. JSON Schema is used to describe the structure and type of JSON data. Like the relationship between DTD and XML.

For the time being, I learned so much about the functions. In short, it has done a very good job in some configuration processing of what is empty processing, date format control, ignoring attribute conversion, and so on. The detailed introduction will be summarized and recorded after the follow-up study.

At this point, I believe you have a deeper understanding of "how to use Json.Net6.0". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report