Recently in the company of a project, which has a part of the request browser side through the jquery Ajax call server side return JSON format of multiple data. Search the internet for a bit, find the following two ways in. NET to generate the JSON.
Method one:. NET Framework 3.0 and earlier versions:
Public Static stringgetjsonstring (DataTable Dt) {string[] Strdc =New string[Dt.Columns.Count]; stringHeadstr =string. Empty; for(inti =0; i < Dt.Columns.Count; i++) {Strdc[i]=Dt.columns[i]. Caption; Headstr+="\""+ Strdc[i] +"\" : \""+ Strdc[i] + i.tostring () +"¾"+"\","; } headstr= Headstr.substring (0, Headstr.length-1); StringBuilder Sb=NewStringBuilder (); Sb.append ("{\""+ Dt.tablename +"\" : ["); for(inti =0; i < Dt.Rows.Count; i++) { stringTempStr =Headstr; Sb.append ("{"); for(intj =0; J < Dt.Columns.Count; J + +) {TempStr= Tempstr.replace (Dt.columns[j] + j.tostring () +"¾", Dt.rows[i][j]. ToString ()); } sb.append (TempStr+"},"); } Sb=NewStringBuilder (Sb.tostring (). Substring (0, Sb.tostring (). Length-1)); Sb.append ("]}"); returnsb.tostring ();}
Method two:. NET 3.5 and later:
Public Static stringgetjsonstring (DataTable dt) {List<Dictionary<string,Object>> rows =Newlist<dictionary<string,Object>>(); foreach(DataRow Drinchdt. Rows) {Dictionary<string,Object> row =Newdictionary<string,Object>(); foreach(DataColumn DCinchdt. Columns) {row. ADD (DC. ColumnName, DR[DC]); } rows. ADD (row); } System.Web.Script.Serialization.JavaScriptSerializer ser=NewSystem.Web.Script.Serialization.JavaScriptSerializer (); returnser. Serialize (rows);}
Method One actually applies to all. NET version, a JSON string is generated manually. Method two was called. NET3.5 the serialize of the newly added JavaScriptSerializer class to convert the output JSON string. Because the DataTable is a. NET data format, it contains a lot of other information (such as tablename,primarykey,constraints, etc.) in addition to the data, so it needs to be converted to dictionary before it can be converted. If the DataTable contains data in the format datetime, then the output JSON string will be "\/date (number of ticks) \ \". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight, January 1, 1970, UTC. In JavaScript, you can call the following function to parse the JSON value:
function Parsejsondate (value) { var A; if (typeof value = = = ' String ') { =/\/date\ ((\d*) \) \//. EXEC (value ); if (a) { returnnew Date (+a[1]); } } return value;}
Or
function Parsejsondate (value) { returnnew Date (parseint (VALUE.SUBSTR (6)));
One thing to note is that Websevice automatically converts the results to JSON-formatted data based on the request, so there is no need to manually serialize the JSON data.
Reference Address:
Http://weblogs.asp.net/navaidakhtar/converting-data-table-dataset-into-json-string
Http://www.telerik.com/forums/consuming-a-net-json-web-service
http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
http://encosia.com/a-breaking-change-between-versions-of-aspnet-ajax/
http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/