C # various application classes of Json strings,
In a program, everything and information can be described and carried by objects. In addition to the popular xml, there is also a simple and fast way to process the target information, that is, Json format.
First, the Json format has its own fixed format: for example, the object "person" can be simply expressed as {"name": "xxxx", "age": 12, "sex ": "male"} format.
Json format description: first use {} to contain the object information to be expressed, use PropertyName: Value in {} to describe the object property, and the Json string can be nested in multiple layers. For example: Json data: [{PropertyName: Value, PropertyName: Value}, {PropertyName: Value, PropertyName: Value}], Json nested format: {PropertyName: Value, PropertyName: {PropertyName: Value, PropertyName: Value} can be infinitely nested theoretically, but it is recommended that no more than three layers be nested here.
After learning about Json format, how can we use Json in C # and associate implemented objects with Json. First, give several namespaces.
Using Newtonsoft. Json;
Using System. Runtime. Serialization;
Using System. ServiceModel;
Using Newtonsoft. Json. Linq;
Using System. IO;
Using System. Runtime. Serialization. Json;
Note that these namespaces are supported in. net3.5 and later versions. You must also add the reference of System. Runtime. Serialization. dll to the reference.
The main classes used here are as follows:
JsonSerializer, StringWriter, StringReader, JsonWriter, JsonConvert, DataContractJsonSerializer。
1. implement mutual conversion between custom classes and 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 Person = new Person ("lanar", "male", 24, "Shaanxi ", 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.Contract Mode: Use DataContractJsonSerializer or JsonReaderWriterFactory provided by System. Runtime. Serialization. dll for implementation
1 [DataContract] 2 public class Person 3 {4 public Person () 5 {6} 7 public Person (string Name, string Sex, int Age, string Address, PersonCharacter Character) 8 {9 this. name = Name; 10 this. sex = Sex; 11 this. age = Age; 12 this. address = Address; 13 this. character = Character; 14} 15 [DataMember] 16 public string Name {get; set;} 17 18 [DataMember] 19 public string Sex {get; set ;} 20 21 [DataMember] 22 public int Age {get; set;} 23 24 [DataMember] 25 public string Address {get; set ;} 26 27 [DataMember] 28 public PersonCharacter Character {get; set;} 29 30} 31 32 public class PersonCharacter33 {34 35 36 public string Daode {get; set ;} 37 38 39 public string Wenhua {get; set;} 40 41 42 public string Xiuyang {get; set;} 43} 44 45 46 public void ShowConvert () 47 {48 Person person = new Person ("Xu zhanpeng", "male", 24, "Shaanxi", new PersonCharacter (); 49 50 Person p = new Person () {Name = "Four Holy Places", Age = 22, Sex = "male", Character = new PersonCharacter () {Daode = "sds", Wenhua = "dasd ", xiuyang = "zzz" }}; 51 DataContractJsonSerializer serializer = new DataContractJsonSerializer (p. getType (); 52 string jsonText; 53 54 try55 {56 using (MemoryStream stream = new MemoryStream () 57 {58 serializer. writeObject (stream, p); 59 jsonText = Encoding. UTF8.GetString (stream. toArray (); 60 Console. writeLine (jsonText); 61} 62 63 using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (jsonText) 64 {65 DataContractJsonSerializer serializer1 = new DataContractJsonSerializer (typeof (Person); 66 Person p1 = (Person) serializer1.ReadObject (MS ); 67 68} 69} 70 catch (Exception ex) 71 {72 73 throw new Exception (ex. message, ex); 74} 75}View Code
When using a contract, you must add the relevant contract modifier to the class and related attributes: [DataContract]. [DataMember] embedded objects do not need to add contract modifiers.
The above is only the most common application method. for implementation with special requirements, you can use the json conversion class implemented by a third party for processing. On the web page, you can use the JavaScriptSerializer class to introduce the System. Web. Script. Serialization namespace for simple Serialization.
JSON Official Website: http://www.json.org/json-zh.html
Independent JSON serialization: http://msdn.microsoft.com/zh-cn/library/bb412170.aspx
How to serialize and deserialize JSON: http://msdn.microsoft.com/zh-cn/library/bb412179.aspx