To say that this function is also because of job needs, the day of work to write a Web page needs Ajax request backstage and data in JSON format to the front-end, due to the company's particularity, can not be connected to the network (very bitter ratio). So only wait until the evening to go home to the internet while checking and writing!
Public classDatatojson {Private StaticSystem.Web.Script.Serialization.JavaScriptSerializer ser =NewSystem.Web.Script.Serialization.JavaScriptSerializer (); //no problem. Public Static stringGetjson (intStatestringmsg) {StringBuilder Jsstr=NewStringBuilder (); Jsstr.append ("{"); Jsstr.appendformat ("\ "State\": {0},\ "msg\": \ "{1}\"", State, MSG); Jsstr.append ("}"); returnjsstr.tostring (); } //no problem. Public Static stringGetjson (stringJsonname,Objecto) {StringBuilder jsstr=NewStringBuilder (); Jsstr.append ("{"); Jsstr.appendformat ("\ ' {0}\ ': {1}", Jsonname, Ser. Serialize (o)); Jsstr.append ("}"); returnjsstr.tostring (); } Public Static stringGetjson (intStatestringJsonname,Objecto) {StringBuilder jsstr=NewStringBuilder (); Jsstr.append ("{"); Jsstr.appendformat ("\ "State\": {0},\ "{1}\": \ "{2}\"", state, Jsonname, Ser. Serialize (o)); Jsstr.append ("}"); returnjsstr.tostring (); } Public Static stringGetjson (intStatestringMsgstringJsonname,Objecto) {StringBuilder jsstr=NewStringBuilder (); Jsstr.append ("{"); Jsstr.appendformat ("\ "State\": {0},\ "msg\": \ "{1}\", \ "{2}\": {3}", State, MSG, Jsonname, ser. Serialize (o)); Jsstr.append ("}"); returnjsstr.tostring (); } }
The above paragraph is I wrote a public method class, mostly still enough JSON format string, wrote a few can use on the overloaded Method!
Public Partial classJsonTest:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { //var rowList<Test> ls =NewList<test>(); Ls. ADD (NewTest () {Sonone ="AA", Sontwo ="AAA", ID =1 }); Ls. ADD (NewTest () {Sonone ="BB", Sontwo ="BBB", ID =2 }); Ls. ADD (NewTest () {Sonone ="CC", Sontwo ="CCC", ID =3 }); Ls. ADD (NewTest () {Sonone ="DD", Sontwo ="DDD", ID =4 }); stringoptions = httpcontext.current.request["Option"];if(Options = ="Search") { varrow = ls. Select (R =New{SO1=R.sonone, SO2=R.sontwo}). ToList (); Response.Write (Common.DataToJson.GetJson (1,"Success","Row", Row)); Response.End (); } } } Public classTest { Public stringSonone {Get;Set; } Public stringSontwo {Get;Set; } Public intID {Get;Set; } }
The above section constructs a jsontest.aspx and then constructs a test data class, here for the convenience of writing directly below. Where row is to construct an anonymous collection for testing purposes.
<%@ Page language="C #"autoeventwireup="true"Codebehind="JsonTest.aspx.cs"inherits="mytest.jsontest"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"> <title></title> <script src="Scripts/jquery-1.10.2.js"></script>"Form1"runat="Server"> <div> <input type="Button"Id="btn"Value="Button"/> </div> </form></body><script type="Text/javascript">$ (function () {$ ('#btn'). Click (function () {varDa ="id=10010"; $.ajax ({type:'GET', data: {Option:"Search"}, URL:'/jsontest.aspx', DataType:'JSON', success:function (Result) {alert (result). State); }, Error:function () {}}); }); });</script>The above is the simple front-end test code!
Of course there are Newtonsoft.Json.dll. This is also very useful, the tutorial online any search can be!
C # convert anonymous classes or collections to JSON format data some methods