ASP. NET built-in object JSON string and object class conversion, asp. netjson
For more information about JSON, google! If I want to write it, I will also copy it after Google! Hey, I have always wanted to learn json, and I have learned a little about it by looking for a lot of materials and writing demos! Get started!
Encapsulate a class first! This category can be found on the Internet! With this class, everything will become simple, haha.
Using System; using System. collections. generic; using System. linq; using System. web; using System. runtime. serialization. json; using System. serviceModel. web; // remember to reference this namespace using System. IO; using System. text; // <summary> /// Summary description for JsonHelper // </summary> public class JsonHelper {public JsonHelper () {// TODO: add constructor logic here // <summary> // serialize the object to a JSON string /// </summary> /// <typeparam name = "T"> Object type </typeparam> /// <param name = "obj"> Object entity </param> /// <returns> JSON string </returns> public static string GetJson <T> (T obj) {// remember to add reference System. serviceModel. web/*** if you do not add the above reference, System. runtime. serialization. json; Json is the bytes of the Oh **/DataContractJsonSerializer json = new DataContractJsonSerializer (typeof (T); using (MemoryStream MS = new MemoryStream () {json. writeObject (MS, obj); string szJson = Encoding. UTF8.GetString (ms. toArray (); return szJson ;}} /// <summary> /// restore the JSON string to an object // </summary> /// <typeparam name = "T"> Object Type </typeparam>/ // <param name = "szJson"> JSON string </param> // <returns> Object entity </returns> public static T ParseFormJson <T> (string szJson) {T obj = Activator. createInstance <T> (); using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (szJson) {DataContractJsonSerializer dcj = new DataContractJsonSerializer (typeof (T); return (T) dcj. readObject (MS );}}}
Test object class:
public class TestData{ public TestData() { } public int Id { get; set; } public string Name { get; set; } public string Sex { get; set; }}
Test page:
<% @ Page Language = "C #" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Script runat =" server "> protected void Page_Load (object sender, EventArgs e) {string jsonStr = string. empty; List <TestData> tds = new List <TestData> (); // test data for (int I = 1; I <4; I ++) {tds. add (new TestData () {Id = I, Name = "jinho" + I, Sex = "male "});} // convert a list to a json string jsonStr = JsonHelper. getJson <List <TestData> (tds); Response. write (jsonStr); this. page. clientScript. registerStartupScript (this. getType (), "json", "getJson (" + jsonStr + ");", true );} </script> <script type = "text/javascript"> function getJson (jsonStr) {// use the eval function var json = eval (jsonStr ); // because the list set above is for (var I = 0; I <json. length; I ++) {alert (json [I]. id + "Name:" + json [I]. name) ;}</script>
Please test the conversion of json strings to entities! Everything can be done as long as there is the JsonHelper class above!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.