JSON is a JavaScript object or array-formatted string, and the HTTP protocol cannot pass a JavaScript object, so it is converted to a string for transmission.
Ajax has a de facto data transfer standard JSON when it passes through the process of assembly and parsing if it makes its own format definition. JSON (a standard, like XML, that JSON specifies the format in which an object is saved as a string) serializes a complex object into a string, and then deserializes the string into an object that JavaScript can read in the browser side. JSON is supported in almost all languages. JSON is a standard for representing objects (JS, Java,. net) as strings.
JavaScript object (key value pair) var person= {name: ' Rupeng ', age:8};
JavaScript array: var names = [' Rupeng ', ' QQ ', ' Taobao '];
JavaScript Object array: var persons = [{name: ' Rupeng ', age:8}, {name: ' QQ ', age:15}, {name: ' Taobao ', age:10}];
JavaScript Object association: var p = {name: ' Yzk ', child:{name: ' Timi ', age:1}};
How to convert a JSON string to a JS object: var obj = eval ("A (" +data+ ")"). Examples: ordinary objects, arrays, object arrays, multi-object associations.
(*) security issues, if not jquery, the new version of the browser is natively supported Json.parse, unsupported reference json2.js will be able to
C # will. NET object is serialized as a JSON string: JavaScriptSerializer (). Serialize (P): NET object →json string →javascript object.
JavaScriptSerializer in the System.Web.Extensions.dll. Json.NET
Anonymous classes in C #: var p = new {id=5,name= "Rupeng"} see in fact that a class is actually generated by anti-compilation.
ASHX Code:
Public voidProcessRequest (HttpContext context) {context. Response.ContentType="text/html"; //serializing an array of strings string[] STRs = {"www.rupeng.com","www.baidu.com","www.qq.com" }; JavaScriptSerializer JSS=NewJavaScriptSerializer (); stringJSON =JSS. Serialize (STRs); Context. Response.Write (JSON); //serialization of anonymous classes varp =New{ID =0, Name ="SYFPC", Height = the }; JavaScriptSerializer JSS=NewJavaScriptSerializer (); stringJSON =JSS. Serialize (P); Context. Response.Write (JSON); //serializing the list collectionlist<string> list =Newlist<string>(); List. ADD ("AAA"); List. ADD ("BBB"); List. ADD ("CCC"); JavaScriptSerializer JSS=NewJavaScriptSerializer (); stringJSON =JSS. Serialize (list); Context. Response.Write (JSON);}
JavaScript code:
<script type= "Text/javascript" > $ (function () { Myajax ("Jsontest.ashx" , function (restxt) { var strs = eval ("(" + Restxt + ")"); // alert (STRs. Name);//Gets the value of the class for (var i = 0; i < strs.length; i++) { alert (Strs[i]) ; } function (status) { } ) }); </script>
JSON introduction and conversion data examples