A Data Transmission Mode in Ajax: JSON

Source: Internet
Author: User

 

What is JSON

JSON is a lightweight data format based on a subset of JavaScript syntax, that is, array and object representation. Because JavaScript syntax is used, JSON definitions can be included in JavaScript files, and access to them does not need to be parsed in XML-based languages. However, before using JSON, it is very important to understand the special Syntax of arrays and object literally in Javascript.

1.1 array literal

Array literal, which is a set of JavaScript values separated by commas (,) enclosed by square brackets. For example:

VaR as = ["hi", 10, true, null];

1.2 object literal

The object literal is defined by two curly braces. You can place any number of "name-value" pairs in curly brackets to define the lattice string value ". Except the last line, each "name-value" pair must be followed by a comma (which is similar to the definition of the Union array in Perl ). For example:

VaR ocar = {

"Color": "green ",

"Doors": 3,

"Paidfor": false

};

1.3 mixed literal

We can mix objects and array literal values to create an array of objects or an object containing arrays. (I am not sure about the specifics)

Someone may be unfamiliar with text in JSON format. I will write JSON data for you below:

{

"Planet ":

{

"Name": "Earth ",

"Type": "small ",

"Info": ["Earth is a small planet, third from the sun", "two-thirds", "climated and landcapes"]

}

}

The following is a simple example:

Function showinfo (Event)

{

VaR planet = This. ID;

VaR scripturl = planet + ". JSON ";

New net. contentloader (scripturl, parsejson );

}

 

Function parsejson ()

{

VaR name = "";

VaR descrip = "";

VaR jsontxt = net. Req. responsetext;

VaR jsonobj = eval ("(" + jsontxt + ")");

Name = jsonobj. Planet. Name

VaR ptype = jsonobj. Planet. type;

If (ptype)

{

Descrip + = "<H2> '+ ptype +" </H2> ";

}

VaR Infos = jsonobj.planet.info;

Descrip + = "<ul> ";

For (var I in Infos)

{

Descrip + = "<li>" + Infos [I] + "</LI> \ n ";

}

Descrip + = "</ul> ";

Top. showpopup (name, descrip );

}

We use contentloader again to obtain data and assign a callback function parsejson (). The entire response text is a legal JavaScript statement, so we can use a simple call to the eval () function to create an object graph:

 VaR jsonobj = eval ("(" + jsontxt + ")");

 

 

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.