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

What is JSON serialization and deserialization

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

Share

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

This article mainly explains "what is JSON serialization and deserialization". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "what is JSON serialization and deserialization".

Method 1: introduce the System.Web.Script.Serialization namespace and use the JavaScriptSerializer class to implement a simple serialization serialization class: Personnel

The copy code is as follows:

Public class Personnel

{

Public int Id {get; set;}

Public string Name {get; set;}

}

Perform serialization deserialization:

The copy code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

Personnel personnel = new Personnel ()

Personnel.Id = 1

Personnel.Name = "rookie"

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer ()

/ / perform serialization

String R1 = jsonSerializer.Serialize (personnel)

/ / perform deserialization

Personnel _ Personnel = jsonSerializer.Deserialize (R1)

}

R1 output result: {"Id": 1, "Name": "rookie"}

You can use the ScriptIgnore attribute tag not to serialize public properties or public fields.

The copy code is as follows:

Public class Personnel

{

[ScriptIgnore]

Public int Id {get; set;}

Public string Name {get; set;}

}

R1 output result: {"Name": "rookie"}

Method 2: introduce the System.Runtime.Serialization.Json namespace and use the DataContractJsonSerializer class to serialize

Serialization class: People

The copy code is as follows:

Public class People

{

Public int Id {get; set;}

Public string Name {get; set;}

}

Perform serialization deserialization

The copy code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

People people = new People ()

People.Id = 1

People.Name = "rookie"

DataContractJsonSerializer json = new DataContractJsonSerializer (people.GetType ())

String szJson = ""

/ / Serialization

Using (MemoryStream stream = new MemoryStream ())

{

Json.WriteObject (stream, people)

SzJson = Encoding.UTF8.GetString (stream.ToArray ())

}

/ / deserialization

Using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (szJson)

{

DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (People))

People _ people = (People) serializer.ReadObject (ms)

}

}

SzJson output result: {"Id": 1, "Name": "rookie"}

You can use IgnoreDataMember: to specify that the member is not part of the data contract and not serialized, DataMember: to define serialization attribute parameters, and to use the DataMember attribute to mark fields that must use the DataContract tag class or the DataMember tag does not work.

The copy code is as follows:

[DataContract]

Public class People

{

[DataMember (Name = "id")]

Public int Id {get; set;}

[IgnoreDataMember]

Public string Name {get; set;}

}

Output result: {"id": 1}

Thank you for reading, the above is the content of "what is JSON serialization and deserialization". After the study of this article, I believe you have a deeper understanding of what JSON serialization and deserialization is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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