Json data conversion, json

Source: Internet
Author: User

Json data conversion, json

Several Methods for json conversion:

Newtonsoft. Json. dll plugin

Reference the dll file in the project.

Json serialization:

String Jsonreslut = JsonConvert. SerializeObject (Entity );

For example:


DataSet ds = schoolbll.GetList(" F_schoolLevel="+Level); string result = JsonConvert.SerializeObject(ds); Response.Write(result); Response.End();

Json deserialization:

String str = "jsonstr ";

Class model = JsonConvert. DeserializeObject <Class> (jsonstr );

For example:

string tasklist = Request.Params["Taskmodel"];        pt.Model.T_Task Taskmodel = JsonConvert.DeserializeObject<pt.Model.T_Task>(tasklist);

 

Json format of string output:

          StringBuilder sbs = new StringBuilder();        sbs.Append("{\"flag\":" + flag + ",\"msg\":\"" + msg + "\"}");        HttpContext.Current.Response.Write(sbs.ToString());        HttpContext.Current.Response.End(); 

Hashtable outputs json to the page:

  public void WriteJSON(Object hs, HttpResponse MyResponse)    {        System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();        StringBuilder json = new StringBuilder();        jss.Serialize(hs, json);        MyResponse.ContentType = "json";        MyResponse.Write(json.ToString());        MyResponse.End();    }

 

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.