The processing of JSON by JS
Json2.js's Source Address:
Https://github.com/douglascrockford/JSON-js
Json2.js provides a way to serialize and deserialize JSON, convert a JSON object to a JSON string, or convert a JSON string to a JSON object.
Add a json2.js reference to the page.
<script type= "Text/javascript" src= "/coreresource/js/json2.min.js" ></script>
Serialization methods
var jsonobj = {"id": "n", "name": "Tom"}; Json.stringify (Jsonobj);
Deserialization method
var jsonstring = "{" id ":" "N", "name": "Tom"} "; Json.parse (jsonstring);
C # Handling of JSON
Newtonsoft.json, a model. NET open source JSON serialization and deserialization class library (http://json.codeplex.com/).
jsonconvert.serializeobject (o) can implement a string that serializes an object into a JSON format
Jsonconvert.deserializeobject () can implement the JSON-formatted string as an object
The following is a simple encapsulation of JSON serialization and deserialization:
<summary>
JSON helper Class
</summary>
public class Jsonhelper
{
<summary>
Serializing objects to JSON format
</summary>
<param name= "O" > Objects </param>
<returns>json string </returns>
public static string SerializeObject (object o)
{
String json = Jsonconvert.serializeobject (o);
return JSON;
}
//<summary>
//Parse JSON string to generate object entity
//</SUMMARY>
//<typeparam name= "T" > Object type </ Typeparam>
//<param name= "JSON" >json string (eg.{" ID ":" "</param>", "Name": "Pebblessintosthe"});
///<returns> object entity </returns>
public static T Deserializejsontoobject<t> (string json) where T:class
{
Jsonserializer serializer = new Jsonserializer ();
StringReader sr = new StringReader (JSON);
Object o = serializer. Deserialize (new JsonTextReader (SR), typeof (T));
T t = o as T;
return t;
}
//<summary>
//Parse JSON array to generate object entity collection
//</summary>
//<typeparam name= "T" > Object type </ Typeparam>
//<param name= "JSON" >json array string (eg.[ {"ID": "Pebblessintosthe", "Name": "{"}]) </param>
//<returns> object entity collection </returns>
public static list<t> Deserializejsontolist <T> (string json) where T:class
{
Jsonserializer serializer = new Jsonserializer ();
StringReader sr = new StringReader (JSON);
Object o = serializer. Deserialize (new JsonTextReader (SR), typeof (List<t>));
List<t> List = o as list<t>;
return list;
}
<summary>
Deserializes the JSON to the given anonymous object.
</summary>
<typeparam name= "T" > Anonymous object Type </typeparam>
<param name= "JSON" >json string </param>
<param name= "Anonymoustypeobject" > Anonymous objects </param>
<returns> Anonymous Objects </returns>
public static T Deserializeanonymoustype<t> (string json, T anonymoustypeobject)
{
T t = Jsonconvert.deserializeanonymoustype (JSON, anonymoustypeobject);
return t;
}
}
<summary>///JSON helper class///</summary> public class Jsonhelper {//<summary> Serializing objects to JSON format///</summary>//<param name= "O" > Objects </param>//<retu Rns>json strings </returns> public static string SerializeObject (object o) {string json = Jso Nconvert.serializeobject (o); return JSON; }///<summary>//Parse JSON string to generate object entity///</summary>//<typeparam name= "T" > Like type </typeparam>///<param name= "JSON" >json string (eg.{" ID ":" "pebblessintosthe", "Name": "{"}) </param>///<returns> object entity </returns> public static T Deserializejs Ontoobject<t> (string json) where T:class {Jsonserializer serializer = new Jsonserializer (); StringReader sr = new StringReader (JSON); Object o = Serializer. Deserialize (new JsonTextReader (SR), typeof (T)); T t = o as T; return t; }///<summary>//Parse JSON array generate object entity collection///</summary>//<typeparam name= "T" > Object type </typeparam>//<param name= "JSON" >json array string (eg.[ {"ID": "Pebblessintosthe", "Name": "{"}]) </param>///<returns> Object entity collection </returns> public static list<t> DESERIALIZEJSONTOLIST&L T T> (string json) where T:class {Jsonserializer serializer = new Jsonserializer (); StringReader sr = new StringReader (JSON); Object o = Serializer. Deserialize (new JsonTextReader (SR), typeof (List<t>)); list<t> list = O as list<t>; return list; }///<summary> deserialize JSON to the given anonymous object. </summary>//<typeparam name= "T" > Anonymous object Type </typeparam>//<param name= "JSON" >JSO N string </param>//<param name= "Anonymoustypeobject" > Anonymous object </PARAM> <returns> Anonymous objects </returns> public static T deserializeanonymoustype<t> (string json, T anonymoust Ypeobject) {T t = Jsonconvert.deserializeanonymoustype (JSON, anonymoustypeobject); return t; } }
To further understand Newtonsoft, some examples of tests have been written:
<summary>//JSON test///</summary> public class Jsontest:irun {public void Run () {Student sdudent = new Student (); Sdudent.id = 1; Sdudent. Name = "Chen Chen"; Sdudent. Nickname = "Pebblessintosthe"; Sdudent. class = new Class () {Name = "CS0216", ID = 0216}; Entity serialization and deserialization of string json1 = Jsonhelper.serializeobject (sdudent); Json1: {"id": 1, "name": "Chen Chen", "nickname": "Pebblessintosthe", "Class": {"id": 216, "name": "CS0216"}} Student Sdudent1 = Jsonhelpe R.deserializejsontoobject<student> (Json1); Entity collection serialization and deserialization list<student> sdudentlist = new list<student> () {sdudent, sdudent1}; String json2 = Jsonhelper.serializeobject (sdudentlist); JSON: [{"id": 1, "name": "Chen Chen", "nickname": "Pebblessintosthe", "Class": {"id": 216, "name": "CS0216"}},{"id": 1, "name": "Chen Chen", "nickname ":" Pebblessintosthe "," Class ": {" ID ": 216," Name ":" CS0216 "}}] list<student> SdudenTList2 = jsonhelper.deserializejsontolist<student> (Json2); DataTable serialization and deserialization of a datatable dt = new DataTable (); Dt. TableName = "Student"; Dt. Columns.Add ("ID", typeof (int)); Dt. Columns.Add ("Name"); Dt. Columns.Add ("nickname"); DataRow dr = dt. NewRow (); dr["ID"] = 112; dr["Name"] = "war Three"; dr["nickname"] = "small three"; Dt. Rows.Add (DR); String json3 = jsonhelper.serializeobject (DT); Json3: [{"ID": "nickname", "Name": "Battle Three", "Small Three"}] DataTable sdudentDt3 = jsonhelper.deserializejsontoobject< ;D atatable> (Json3); list<student> sdudentList3 = jsonhelper.deserializejsontolist<student> (Json3); Validation object and array Student sdudent4 = jsonhelper.deserializejsontoobject<student> ("{\" id\ ": \" 112\ ", \" name\ ": \" pebblessintosthe \ "}"); list<student> SdudentList4 = jsonhelper.deserializejsontolist<student>("[{\" id\ ": \" 112\ ", \" name\ ": \" pebblessintosthe \ "}]"); Anonymous object Resolution var tempentity = new {ID = 0, Name = string. Empty}; String json5 = Jsonhelper.serializeobject (tempentity); Json5: {"ID": 0, "Name": ""} tempentity = Jsonhelper.deserializeanonymoustype ("{\" id\ ": \" 112\ ", \" name\ ": \" pebblessintosthe \ " } ", tempentity); var tempstudent = new Student (); Tempstudent = Jsonhelper.deserializeanonymoustype ("{\" id\ ": \" 112\ ", \" name\ ": \" pebblessintosthe \ "}", tempstudent); Console.read (); }}///<summary>//Student Information entity///</summary> public class Student {public int ID {GE T Set public string Name {get; set;} public string Nickname {get; set;} public class class {get; set;} }///<summary>//Student class entities///</summary> public class class {public int ID {get; set; public string Name {get; set;} }
There are two points to note when using the JSON helper class:
1. It is usually possible to serialize the SerializeObject () and deserialize Deserializejsontoobject () using the calling entity () two methods. However, in some cases when we parse the JSON string, there may be no corresponding entity type (or do not want to add the corresponding entity Class), this time can use the Anonymous object parsing method Deserializeanonymoustype (), convenient and quick, the corresponding code is as follows:
Anonymous object Resolution var tempentity = new {ID = 0, Name = string. Empty}; String json5 = Jsonhelper.serializeobject (tempentity); Json5: {"ID": 0, "Name": ""} tempentity = Jsonhelper.deserializeanonymoustype ("{\" id\ ": \" 112\ ", \" name\ ": \" pebblessintosthe \ "}", tempentity); Console.WriteLine (Tempentity.id + ":" + tempentity.name);
2. Two structure arrays of JSON and object parsing are slightly different. JSON objects are generally converted to entities, and JSON arrays are generally converted into entity collections. The code is as follows:
Validation object and array Student sdudent4 = jsonhelper.deserializejsontoobject<student> ("{\" id\ ": \" 112\ ", \" name\ ": \" Pebblessintosthe \ "}"); list<student> sdudentList4 = jsonhelper.deserializejsontolist<student> ("[{\" id\ ": \" 112\ ", \" name\ ": \" Pebblessintosthe \ "}]");
Explain the meanings of JSON objects and arrays in a nutshell:
The object is terminated with "{" (opening parenthesis), "}" (closing parenthesis). Each "name" is followed by a ":" (colon); "' Name/value ' pairs" are separated by "," (comma). The name is enclosed in quotation marks, and if the value is a string, it must be in parentheses, and the numeric type is not required. For example: {"ID": "" "," "Name": "Pebblessintosthe"}.
An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values. For example: [{"id": "pebblessintosthe", "name": "},{", "id": "113", "name": "Chen Chen"}].
C # JSON processing and JSON serialization in JS