JSON introduction and conversion data examples

Source: Internet
Author: User
Tags format definition javascript array

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

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.