First, the basic concept
What is JSON?
Json:javascript Object Notation (JavaScript object Notation).
JSON is a lightweight data interchange format that is the syntax for storing and exchanging textual information. Similar to XML.
JSON is smaller, faster, and easier to parse than XML.
What's going on with serialization and deserialization?
Serialization: The process of converting a data structure or object into a binary string
Deserialization: The process of converting a binary string generated during serialization into a data structure or object
How to: Serialize and deserialize JSON data?
1. Define the data contract for person
Define a data contract for a person by attaching datacontractattribute to the class and attaching the DataMemberAttribute attribute to the member to serialize.
[DataContract] Internal class person { [DataMember] internalstring name; [DataMember] Internal int Age ; }
2. Serializing an instance of the person type to JSON
3. Deserializing instances of the person type from JSON
For more information:
Https://msdn.microsoft.com/zh-cn/library/bb412179.aspx
Https://msdn.microsoft.com/zh-cn/library/bb412170.aspx
Two. Several analytic JSON methods commonly used in net
. A comparison of several analytic JSON methods commonly used in net
Name |
Class Library |
Usage Scenarios |
LINQ Support |
System.Runtime.Serialization.Json |
DataContractJsonSerializer |
All |
No |
System.Web.Script.Serialization |
JavaScriptSerializer |
Web |
No |
System.json |
Jsonarray, Jsonobject, Jsonvalue |
Silverlight |
Yes |
Newtonsoft.json |
JsonConvert, Jarray, Jobject, Jvalue |
All |
Yes |
1, DataContractJsonSerializer
The. NET Framwork3.5 comes with a System.Runtime.Serialization.Json that does a good job of parsing the JSON without using a third-party library.
Add a reference System.Runtime.Serialization, and then use the using System.Runtime.Serialization.Json;
The corresponding serialized class, note the following class plus attribute, for example:
/// <summary> ///Exception Object/// </summary>[DataContract]Internal classEx {[DataMember] Public stringHelpLink {Get;Set; } [DataMember] Public intHResult {Get;Set; } [DataMember] Public stringMessage {Get;Set; } [DataMember] Public stringSource {Get;Set; } [DataMember] Public stringStackTrace {Get;Set; } }
JSON helper Class
/// <summary> ///JSON helper Class/// </summary> Internal classJsonhelper {//JSON string Goto object Public StaticT jsontot<t> (stringJSON) { varSer =NewDataContractJsonSerializer (typeof(T)); varstream =NewMemoryStream (Encoding.UTF8.GetBytes (JSON)); Stream. Position=0; return(T) ser. ReadObject (stream); } //object into a JSON string Public Static stringTtojson<t>(T obj) {varSer =NewDataContractJsonSerializer (typeof(T)); varstream =NewMemoryStream (); Ser. WriteObject (stream, obj); vardb =New byte[Stream. Length]; Stream. Position=0; Stream. Read (DB,0, (int) stream. Length); varDatastring =Encoding.UTF8.GetString (DB); returndatastring; } }
Use:
var t = jsonhelper.jsontot<t>var jsonstr=jsonhelper.ttojson<t> (t);
For more information, please refer to: https://msdn.microsoft.com/zh-cn/library/bb907644.aspx
2, JavaScriptSerializer
Using System.Web.Script.Serialization;
var jsserializer new JavaScriptSerializer (); var json= jsserializer.serialize (T); var T = jsserializer.deserialize<t> (JSON);
For more information, please refer to: http://msdn.microsoft.com/zh-cn/library/bb359469.aspx
3, System.json
//using System.json varCSS ="{\ "#header \": {background:\ "red\"}, Layout: [5,4,1],color:\ "cyan\"}"; varstyle = Jsonobject.parse (CSS) asJsonobject; ( fromSinchstylewhereS.key = ="Color" Select(string) (S.value). First (). ToString (); //"Cyan"//More Operationsstyle["Layout"][0] = A; varHD = style["#header"]; style["body>div+p"] =HD; Style. Remove ("#header"); varBD =NewJsonobject (); bd["Border"] ="1px solid Cyan"; style["body>div+p"]["#meta"] =BD; Style. ToString (); //{"Layout": [22,4,1], "color": "Cyan", "body>div+p": {"background": "Red", "#meta": {"Border": "1px solid Cyan"}}}
For more information, please refer to: http://msdn.microsoft.com/zh-cn/library/cc626400%28v=VS.95%29.aspx
4, Json.NET
JSON helper Class
Public classJsonhelper {/// <summary> ///Convert object to JSON string/// </summary> /// <param name= "item" ></param> /// <returns></returns> Public Static stringObjecttojson (ObjectItem) { stringres =jsonconvert.serializeobject (item); returnRes; } /// <summary> ///convert JSON to Object/// </summary> /// <typeparam name= "T" ></typeparam> /// <param name= "jsonstring" ></param> /// <returns></returns> Public StaticT jsontoobject<t> (stringjsonstring) {T res= jsonconvert.deserializeobject<t>(jsonstring); returnRes; } }
For more information, please refer to: http://www.newtonsoft.com/json/help/html/Introduction.htm
Third, add concern
If this article is helpful to you, please click on " good text to top " and " Follow me " in the bottom right corner.
. Several analytic JSON methods commonly used in net