Asp. NET self-with object JSON string and entity class conversion _ Practical Tips

Source: Internet
Author: User
Tags eval serialization

For more information on JSON, please Google understand! If you want me to write, I also go to Google after copy! Hey, always want to learn json, a lot of looking for information and write demo, finally a little understand! Cut to the Chase!

Let's first encapsulate a class! This kind of online can be found! There's this class, everything will be easy, 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 Jsonhel Per () {/////Todo:add constructor logic-here//}///<summary>///serializes the object to the 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.servicemo Del. Web/** * If the above reference is not added, System.Runtime.Serialization.Json;
    JSON doesn't come out. * * * * * * * * * 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>///restores JSON string to object///</summary>///<typeparam name= "T" > Object type </typeparam&
  Gt <param name= "Szjson" >json string </param>///<returns> object entity </returns> public static T Parseform
    Json<t> (String szjson) {T obj = activator.createinstance<t> (); using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (Szjson)) {DataContractJsonSerializer DCJ = n
      EW DataContractJsonSerializer (typeof (T)); Return (T) DCJ.
    ReadObject (MS);
 }
  }
}

To test an entity 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 = Strin
    G.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 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) {//using the Eval function var json = eval     (JSONSTR); Because the list is set for (var i = 0; i < json.length i++) {alert (json[i). Id + "Name:" + json[i].
    Name); }} </script>  

About JSON string conversions to entities please test it yourself! As long as there is the Jsonhelper class, everything is good to do!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.