1. Recently, the project has been on the line, spare a few days to the JSON serialization and deserialization to re-encapsulate the I defined as JSONHELP, although Microsoft has done very well. But I want to encapsulate a set of projects that I've developed for my own use. Convenient for later expansion and development use.
2. What is JSON?
json:javascript Object Notation (javaobject notation). JSON is the syntax for storing and exchanging textual information. Similar to XML. JSON is smaller, faster, and easier to parse than XML. Now it is essential to develop Web application JSON. JSON is a lightweight text data exchange, JSON is independent of language, JSON is self-descriptive, easier to understand ,json use JavaScript syntax to describe data objects, but JSON is still independent of language and platform. The JSON parser and the JSON library support many different programming languages.
3.JSON-Convert to JavaScript object
The JSON text format is syntactically identical to the code that creates the JavaScript object. Because of this similarity, the JavaScript program can use the built-in eval () function to generate native JavaScript objects with JSON data without a parser.
4. XML-Like
JSON is a self-describing feature of JSON, JSON has a hierarchical structure (values exist in value) JSON can be parsed through JavaScript JSON data can be transmitted using AJAX compared to XML differences without end tag shorter read and write Faster can be resolved using the built-in, JavaScript eval () method, using arrays, without using reserved words
5. Why use JSON?
JSON is faster and easier to use than XML for AJAX applications: Reading XML documents using XML uses XML DOM to iterate through the document read values and store them in variables
6. Using JSON
Reading JSON strings using eval () to process the JSON string front-end JS interface We typically use, json.parse () to serialize an object or string to JSON, and json.stringify () to serialize to a string.
7. Serialization and deserialization of JSON
JSON serialization and deserialization I use Jsonconvert.serializeobject () and using System.Runtime.Serialization.Json among the using Newtonsoft.json; Among the Jil.JSON.Serialize ()
Of course, the 2 serialization methods are the same. But the efficiency of Jil.JSON.Serialize () serialization is indeed much higher than this jsonconvert.serializeobject (). It is clearly visible when a large amount of data is serialized. So
I recommend that you use Jil.JSON.Serialize () to serialize objects and deserialize objects, and so on, when serializing and deserializing the amount of data is very large and many times.
The 9.jsonhelp.cs code is as follows:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
1 using newtonsoft.json; 2 using system; 3 using System.IO; 4 using System.Runtime.Serialization.Json; 5 using system.text; 6 /************************************************ 7 ◇ Lowkeyc 8 ◇ illustrates the serialization and deserialization of JSON in: General management 9 ◇ version number:v1.0 10 ◇ Date of creation: June 22, 2017 Thursday 11 *****************************************************/ 12 namespace rapiddevelopmentframework.common 13 { 14 /// <summary> 15 /// json Auxiliary class 16 /// </summary> 17 public static class jsonhelper 18 { 19 20 /// <summary> 21 /// JSON Normal version of serialization 22 /// </ Summary> 23 /// <typeparam name= "T" ></typeparam> 24 /// <param Name= "_object" ></param> 25 /// < Returns></returns> 26 public static String ObjectToJSON<T> (This t _object) where T :class 27 { 28 return jsonconvert.serializeobject (_object); 29 &nbsP;} 30 31 /// <summary> 32 /// JSON Standard version of serialization 33 /// </summary> 34 /// <param name= "_object" ></param> 35 /// <returns></returns> 36 public static string objecttojson (This object _object) 37 { 38 return jsonconvert.serializeobject (_Object); 39 } 40 41 42 /// <summary> 43 /// JSON official version of serialization 44 /// </summary> 45 /// <typeparam name= "T" ></typeparam> 46 /// <param name= "_Object" ></param> 47 /// <returns></returns> 48 public static string objecttojsonofficial<t > (this t _object) where T :class 49 { 50 using (Var tempmemorystream = new memorystream ()) 51 { 52 var mydatacontractjsonserializer = new datacontractjsonserializer ( typeof (T)); 53 mydatacontractjsonserializer.writeobject (Tempmemorystream, _object); 54 var Myserializationstring = encoding.utf8.getstring (Tempmemorystream.toarray ()); 55 return myserializationstring; 56 } 57 } 58 59 60 /// <summary> 61 ///JSON serialized version of jil 62 /// </summary> 63 /// <typeparam name= "T" ></typeparam> 64 /// <param name= "_object" ></param> 65 /// <returns></returns> 66 public static String ObjectToJSONJil<T> (This t _object) where T :class 67 { 68 return jil.json.serialize (_Object, Jil.Options.ExcludeNullsIncludeInherited); 69 } 70 71 72 /// <summary> 73 /// JSON serialized version of Jil can contain null 74 /// </summary> 75 /// <typeparam name= "T" ></ Typeparam> 76 /// <param name= "_ Object "></param> 77 /// <returns ></returns> 78 public static String objecttojsonjilincludenulls<t> (This t _object) where t:class 79 &nbsP; { 80 return jil.json.serialize (_object, jil.options.includeinherited); 81 } 82 83 84 /// <summary> 85 /// JSON deserialization 86 /// </summary> 87 /// <typeparam name= "T" ></typeparam> 88 /// <param name= "_jsonstring" ></param> 89 /// <returns></returns> 90 public stAtic t jsontoobjectjson<t> (this string _jsonstring) where T:class 91 { 92 return JsonConvert.DeserializeObject<T> (_jsonstring); 93 } 94 95 96 /// < Summary> 97 /// json official version of the deserialization 98 /// </summary> 99 /// <typeparam name= "T" ></typeparam>100 /// <param name= "_jsonstring" ></param>101 /// <returns></returns>102 Public static t jsontoobjectofficial<t> (this string _jsonstring) where T :class103 {104 using (var tempmemorystream = new MemoryStream (Encoding.UTF8.GetBytes (_jsonstring))) 105 {106 var mydatacontractjsonserializer = new datacontractjsonserializer ( typeof (T));107 return (T) mydatacontractjsonserializer.readobject (tempmemorystream);108 }109 }110 111 112 /// The official version of <summary>113 /// JSON 's deserialization 114 /// </summary>115 /// <typeparam name= "T" ></typeparam>116 /// <param name= "_JSONString" ></param >117 /// <returns></returns>118 public static T JSONToObjectJil<T> (This string _jsonstring) where T :class119 {120 using (var InputString = new stringreader (_jsonstring)) 121 {122 var ObjectResult = Jil.JSON.Deserialize<T> (inputstring);123 return ObjectResult; 124 }125 }126 127 128 /// <summary>129 /// JSON jil version of deserialization which can contain null 130 /// </summary>131 /// < Typeparam name= "T" ></typeparam>132 /// <param name= "_jsonstring" ></param>133 /// <returns></returns>134 public static T JSONToObjectJilIncludeNulls<T> (this string _jsonstring) where T :class135 {136 using (var inputstring = new StringReader (_jsonstring)) 137 { 138 var objectresult = jil.jSON. Deserialize<t> (inputstring, jil.options.includeinherited);139 return ObjectResult;140 }141 }142 143 }144 }
Generic encapsulation based on JSON serialization and deserialization