Get the App
SLTechnology News&Howtos  ›  Development  › 

How to serialize and deserialize Json in C #

Shulou Source: shulou.com Published: 2022-05-31 20:01:28 09月22日 Update

Most people do not understand the knowledge points of this article "how to serialize and deserialize Json", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "C# how to serialize and deserialize Json" article.

A brief introduction to Json

Json (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JS. Json uses a completely language-independent text format. This makes Json an ideal data exchange language. It is easy for people to read and write, and it is also easy for machine parsing and generation.

Json is simply objects and arrays in JS, so Json also has two structures: objects and arrays.

Json object: the Json object is defined in curly braces "{}" and stores data in the form of Key:value key-value pairs, multiple data using semicolons ";" segmentation.

Serialization Object obj = Serialization.JsonToObject (strJson); deserialization strJson = Serialization.ObjectToJSON (obj); fourth, tool class public static class Serialization {public static T JsonToObject (string jsonText) {DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer (typeof (T)); MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (jsonText)); T result = (T) ((object) dataContractJsonSerializer.ReadObject (memoryStream)) MemoryStream.Dispose (); return result;} public static string ObjectToJSON (T obj) {DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer (typeof (T)); string result = string.Empty; using (MemoryStream memoryStream = new MemoryStream ()) {dataContractJsonSerializer.WriteObject (memoryStream, obj); memoryStream.Position = 0L Using (StreamReader streamReader = new StreamReader (memoryStream)) {result = streamReader.ReadToEnd ();}} return result;}} JSONStrToList

Custom model

Public class Obj {public string Name {get; set;} public double Price {get; set;}}

JSONStrToList

/ / json converts objects, arrays, deserializes public static void JSONStringToList () {/ / json format string string JsonStr = "{Name:' Apple', Price:5.5}"; JavaScriptSerializer Serializer = new JavaScriptSerializer (); / / json string turns into objects, deserializes Obj obj = Serializer.Deserialize (JsonStr) Console.Write (obj.Name + ":" + obj.Price + "\ r\ n"); / / json format string string JsonStrs = "[{Name:' Apple', Price:5.5}, {Name:' Orange', Price:2.5}, {Name:' Persimmon', Price:16}]"; JavaScriptSerializer Serializers = new JavaScriptSerializer () / / the json string is converted to an array object, deserializing List objs = Serializers.Deserialize (JsonStrs); foreach (var item in objs) {Console.Write (item.Name + ":" + item.Price + "\ r\ n") } StrTosJSON public static JObject strToJson (string jsonText) {jsonText = "{\" shenzheng\ ":\" Shenzhen\ ",\" beijing\ ":\" Beijing\ ",\" shanghai\ ": [{\" zj1\ ":\" zj11\ ",\" zj2\ ":\" zj22\ "},\" zjs\ "]}"; JObject jo = (JObject) JsonConvert.DeserializeObject (jsonText) / / or / JObject jo = JObject.Parse (jsonText); string zone = jo ["shenzheng"] .ToString (); / / output "Shenzhen" string zone_en = jo ["shanghai"] .ToString (); / / output "[{" zj1 ":" zj11 "," zj2 ":" zj22 "}," zjs "]" string zj1 = jo ["shanghai"] [1] .ToString () / / output "zjs" Console.WriteLine (jo); return jo;} above is about "how C # serializes and deserializes Json". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Tags: Sequence object content character string data array format caching output that is article knowledge article Apple language Shenzhen value semicolon and at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Docker MariaDB OPPO Reno