ASP. NET built-in object JSON string and object class conversion, asp. netjson

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.