In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.