C#中Json字符串的各類運用類實例講授。本站提示廣大學習愛好者:(C#中Json字符串的各類運用類實例講授)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中Json字符串的各類運用類實例講授正文
在法式中任何事物及信息都可以用對象停止描寫和承載,除比擬風行的xml以外,還有一種簡練疾速處置目的信息的情勢那就是Json格局。
起首Json格局有本身固定的格局:例如person這個對象便可以簡略的表現成{"name":"xxxx","age":12,"sex":"男"}情勢。
Json格局的描寫以下:先用{}包括須要表達的對象信息,在{}裡應用PropertyName:Value停止對象屬性的描寫,同時Json字符串可以完成多層嵌套。例如:Json數據:[{PropertyName:Value,PropertyName:Value},{PropertyName:Value,PropertyName:Value}],Json嵌套格局:{PropertyName:Value,PropertyName:{PropertyName:Value,PropertyName:Value}},實際上可以無窮嵌套,然則這裡建議嵌套最好不要跨越3層。
對Json格局有了懂得以後,那我們若何在C#中應用Json和將曾經完成的對象與Json接洽起來。先個給出幾個定名空間
using Newtonsoft.Json;
using System.Runtime.Serialization;
using System.ServiceModel;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Runtime.Serialization.Json;
這裡須要留意的是 這些定名空間在 .net3.5及以上本版支撐,同時必需在援用中添加 System.Runtime.Serialization.dll的援用。
這裡重要應用到的類以下:
JsonSerializer ,StringWriter,StringReader ,JsonWriter,JsonConvert, DataContractJsonSerializer。
1.完成自界說類與Json停止互相轉化處置:
public class Person { public Person() { } public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character) { this.Name = Name; this.Sex = Sex; this.Age = Age; this.Address = Address; this.Character = Character; } public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; } public string Address { get; set; } public PersonCharacter Character { get; set; } } public class PersonCharacter { public string Daode { get; set; } public string Wenhua { get; set; } public string Xiuyang { get; set; } } public void ShowConvert() { Person person = new Person("lanar", "男", 24, "陝西", new PersonCharacter()); JsonSerializer serializer = new JsonSerializer(); StringWriter sw = new StringWriter(); serializer.Serialize(new JsonTextWriter(sw), person ); Console.WriteLine(sw.GetStringBuilder().ToString()); StringReader sr = new StringReader(@"{""Name"":""ppp"", ""Age"":"12"}"); Person p1 = (Project)serializer.Deserialize(new JsonTextReader(sr), typeof(Person)); Console.WriteLine(p1.Name+ "=>" + p1.Age); }
2. 契約方法:應用System.Runtime.Serialization.dll供給的DataContractJsonSerializer或許 JsonReaderWriterFactory完成
public class Person { public Person() { } public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character) { this.Name = Name; this.Sex = Sex; this.Age = Age; this.Address = Address; this.Character = Character; } [DataMember] public string Name { get; set; } [DataMember] public string Sex { get; set; } [DataMember] public int Age { get; set; } [DataMember] public string Address { get; set; } [DataMember] public PersonCharacter Character { get; set; } } public class PersonCharacter { public string Daode { get; set; } public string Wenhua { get; set; } public string Xiuyang { get; set; } } public void ShowConvert() { Person person = new Person("許展鵬", "男", , "陝西", new PersonCharacter()); Person p = new Person() { Name = "四年夜聖地", Age = , Sex = "男", Character = new PersonCharacter() { Daode="sds", Wenhua="dasd", Xiuyang="zzz"} }; DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType()); string jsonText; try { using (MemoryStream stream = new MemoryStream()) { serializer.WriteObject(stream, p); jsonText = Encoding.UTF.GetString(stream.ToArray()); Console.WriteLine(jsonText); } using (MemoryStream ms = new MemoryStream(Encoding.UTF.GetBytes(jsonText))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person)); Person p = (Person)serializer.ReadObject(ms); } } catch (Exception ex ) { throw new Exception(ex.Message,ex); } }
應用契約方法必需在類及相干的屬性添加相干的契約潤飾符: [DataContract], [DataMember] 內嵌的對象外部可以不消添加契約潤飾符號的。
以上只是最經常使用的運用方法,關於有特別需求的完成可使用第三方完成的json轉換類停止處置。在web頁面可使用 引入System.Web.Script.Serialization定名空間應用 JavaScriptSerializer類完成簡略的序列化。